<script language="javascript">
function validate(frm) {
var inputFields = new Array("First Name" ,"Last Name" ,"Company" ,"Address1" ,"City" ,"Zip" ,"Country" ,"Phone" ,"formmail_mail_email" ,"Return Quantity 1" ,"Purchase Order 1" ,"Part Number 1" ,"Reason for Return 1");
var counter;
var name;
var msg = "Please complete the following fields:\n";
var badFields = "";
for (counter = 0; counter < inputFields.length; counter++) {
name = inputFields[counter];
if (frm.elements[name].value.length == 0) {
if (name == "formmail_mail_email") {
badFields = badFields + " - Email \n";
} else {
badFields = badFields + " - " + name + "\n";
}
}
}
if (badFields.length != 0) {
alert(msg + badFields);
return false;
}
if (frm.formmail_mail_email.value.length > 0) {
return emailCheck(frm.formmail_mail_email.value);
} else {
return true;
}
if (frm.terms.checked) {
return true;
} else {
alert('You must agree to the Terms and Conditions');
return false;
}
}
function emailCheck(emailStr) {
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null) {
alert("The username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray==null) {
alert("The domain name doesn't seem to be valid.");
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
alert("The address must end in a three-letter domain, or two letter country.");
return false;
}
if (len<2) {
var errStr="This address is missing a hostname!";
alert(errStr);
return false;
}
return true;
}
</script>
As a general rule, don't use return true, use only return false. No matter the values returned, the return command inside a function will stop the code execution, so that when you insert a return true, the rest of the code will not be run. Don't worry: if you don't specify a return value, the function will return itself as value. From a Boolean point of view any other value returned, except false, null or 0, will be equivalent with true.
Thus, instead of:
Code:
if (frm.formmail_mail_email.value.length > 0) {
return emailCheck(frm.formmail_mail_email.value);
} else {
return true; // here the code will stop anyway
}
if (frm.terms.checked) {
return true;
} else {
alert('You must agree to the Terms and Conditions');
return false;
}
Try:
Code:
if (frm.formmail_mail_email.value.length > 0) {
emailCheck(frm.formmail_mail_email.value);
}
if (!frm.terms.checked) {// if NOT
alert('You must agree to the Terms and Conditions');
return false;
}
You should probably change the emailCheck() function code accordingly
Same warning applies as in the other post! By setting the value="" I think you may be breaking the checkbox. Take that out and the value will be "yes" if it is checked or "no" if not. You can then validated with the IF statement
As a general rule, don't use return true, use only return false. No matter the values returned, the return command inside a function will stop the code execution, so that when you insert a return true, the rest of the code will not be run. Don't worry: if you don't specify a return value, the function will return itself as value. From a Boolean point of view any other value returned, except false, null or 0, will be equivalent with true.
Thus, instead of:
Code:
if (frm.formmail_mail_email.value.length > 0) {
return emailCheck(frm.formmail_mail_email.value);
} else {
return true; // here the code will stop anyway
}
if (frm.terms.checked) {
return true;
} else {
alert('You must agree to the Terms and Conditions');
return false;
}
Try:
Code:
if (frm.formmail_mail_email.value.length > 0) {
emailCheck(frm.formmail_mail_email.value);
}
if (!frm.terms.checked) {// if NOT
alert('You must agree to the Terms and Conditions');
return false;
}
You should probably change the emailCheck() function code accordingly
Doing what you suggested rendered the entire validation function invalid. Even the required input text boxes no longer worked.
This shouldn't be that hard, but for some reason, it is...
Bookmarks