Debt Consolidation - Submit articles - Air Jordans - Property in Bulgaria - Credit Card

PDA

View Full Version : Help combining two functions.


pictureboarduk
Feb 23rd 2009, 6:46 pm
Hi,

I am trying to combine the two pieces of code below for a form validation script, but am having some difficulty.

Check the form field is not empty:

//start check subject is not empty
function validateMessage(fld) {
var error = "";

if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The Message has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}
//end check subject is not empty

Check the field contains no special characters:

var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

for (var i = 0; i < document.formname.fieldname.value.length; i++) {
if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
return false;
}
}


At the top of the file, I am using:

reason += validateMessage(theForm.message);//check message


I want to both check the field is not empty, and also prevent special characters being inserted for security, but am not able to figure out how to combine the two.

If anyone would be sop kind as to help me, I would be most appreciative.

Thanks :)

yoavmatchulsky
Feb 23rd 2009, 10:29 pm
function validateMessage(fld) {
var error = "";

if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The Message has not been filled in.\n"
} else {
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

for (var i = 0; i < fld.value.length; i++) {
if (iChars.indexOf(fld.value.charAt(i)) != -1)
{
error = "Your username has special characters. \nThese are not allowed.\n Please remove them and try again.";
break;
}
}
}

if (error == "")
{
fld.style.background = 'White';
}

return error;
}