atrum Posted March 7, 2009 Share Posted March 7, 2009 Hey guys, I need a little bit of help with an odd problem when validating a form in javascript. The issue is that the form submits before the javascript finishes validating all fields. It will validate up to compairing the two emails fields. txt_email, and txt_email_c, then it skips all other fields and submits the form. Here is my code. function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } function checkemail(field1,field2,alerttxt) { with (field1,field2) { if (field1.value != field2.value) {alert(alerttxt);return false;} else {return true;} } } function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false;} else {return true;} } } function validate_form(thisform) { with(thisform) { if (validate_required(txt_firstname,"Firstname must be filled out!")==false) {txt_firstname.focus();return false;} if (validate_required(txt_lastname,"Lastname must be filled out!")==false) {txt_lastname.focus();return false;} if (validate_required(txt_email,"Email must be filled out!")==false) {txt_email.focus();return false;} if (validate_email(txt_email,"Not a valid email!")==false) {txt_email.focus();return false;} if (validate_required(txt_email_c,"Email must be confirmed!")==false) {txt_email_c.focus();return false;} if (checkemail(txt_email_c,txt_email,"Emails do not match!")==false) {txt_email_c.focus();return false;} if (validate_required(txt_dob,"Username must be filled out!")==false) {txt_birthdate.focus();return false;} if (validate_required(txt_password,"Password must be filled out!")==false) {txt_password.focus();return false;} if (validate_required(txt_password_c,"Password must be confirmed!")==false) {txt_password_c.focus();return false;} } } any help any one can provide in trouble shooting this issue would be appreciated. Link to comment https://forums.phpfreaks.com/topic/148336-solved-form-validation-submitting-before-script-finishes/ Share on other sites More sharing options...
Psycho Posted March 7, 2009 Share Posted March 7, 2009 I don't believe you can use two variables in a with() statement. Link to comment https://forums.phpfreaks.com/topic/148336-solved-form-validation-submitting-before-script-finishes/#findComment-778772 Share on other sites More sharing options...
atrum Posted March 7, 2009 Author Share Posted March 7, 2009 hmmmmm. now that I think about it, I haven't tried just an if statement instead of with(). Link to comment https://forums.phpfreaks.com/topic/148336-solved-form-validation-submitting-before-script-finishes/#findComment-778819 Share on other sites More sharing options...
atrum Posted March 7, 2009 Author Share Posted March 7, 2009 Hey guys, I acutally got it resolved. I used a while() statement in the email comparison check instead of a with() function validate_match(field1,field2,alerttxt) { while (field1,field2) { if (field1.value != field2.value) {alert(alerttxt);return false;} else {return true;} } } if (validate_match(txt_email_c,txt_email,"Emails do not match!")==false) {txt_email.focus();return false;} Link to comment https://forums.phpfreaks.com/topic/148336-solved-form-validation-submitting-before-script-finishes/#findComment-779094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.