I've written a validation script to validate forms before submission. One form is a subscription form (requiring only name and email), while the other is a full-fledged contact form. The script works for both the name and email of the mailing list form, but still submits the form after clicking ok on both alerts. The contact form, on the other hand, throws the alert for the first field (name), then submits the form without going through the other fields.
Below is the javascript, then the two form lines where I call the functions. Help!
Mailing list form:Code:function validEmail(fieldName){ var validate = fieldName.value; if(validate == null || validate == ''){ var apos = validate.indexOf("@"); var dotpos = validate.lastIndexOf("."); if (apos < 1|| dotpos - apos < 2){ alert('Please enter a valid email address!'); return false; } else{ return true; } } } function validForm(fieldName2,fieldLoc,fieldType){ var validate = fieldLoc.value; if(validate == null || validate == ''){ alert('Please enter a valid ' + fieldName2); return false; } else{ if(fieldType == 'number'){ var validateNum = validate.replace(/[\(\)\.\-\ ]/g, ''); if(isNaN(parseInt(validateNum))){ alert('Please enter a valid ' + fieldName2); return false; } else{ return true; } } else if(fieldType == 'email'){ validEmail(fieldLoc); } else{ return true; } } } function validFormRun(){ validForm('name',this.name,'text'); validForm('address',this.address,'text'); validForm('city',this.city,'text'); validForm('message',this.message,'text'); validEmail(this.email); validForm('phone number',this.phone,'number'); validForm('zip code',this.zip,'number'); }
Contact form:Code:<form action="forms.php" method="post" onsubmit="validForm('email address',this.mailing_email,'email'); validForm('cell phone number',this.mailing_cell,'number');">
Thanks a lot!Code:<form method="post" action="forms.php" id="contactForm" name="contactForm" onsubmit="return validFormRun()">



Reply With Quote

Bookmarks