highandwild Posted January 13, 2011 Share Posted January 13, 2011 Hello everyone. My first POST of many maybe as I've just found this forum and whilst I do try and sort problems out before I ask for help I need to avoid stress and frustration. I just need to validate certain fields on a form when the user presses the Submit button. The individual validation functions below work fine but I do not know how to check the email address , alert if invalid and set focus onto the field but then check the fname if the email address is valid and so on. Any ideas someone or is there a better way of doing this? Thanks Justin function validate_form(thisform) { with (thisform) { if (validate_emailadd(emailadd,"Not a valid e-mail address!")==false) {emailadd.focus;return false;} if (validate_fname(fname,"Please enter your forename.")==false) {fname.focus();return false;} if (validate_sname(sname,"Please enter your surname.")==false) {sname.focus();return false;} } } function validate_fname(field,alerttxt) { with (field) { lenoftxt=value.length; if (lenoftxt=0) {alert(alerttxt);return false;} else {return true;} } } function validate_sname(field,alerttxt) { with (field) { lenoftxt=value.length; if (lenoftxt=0) {alert(alerttxt);return false;} else {return true;} } } function validate_emailadd(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } Quote Link to comment https://forums.phpfreaks.com/topic/224300-validating-multiple-form-fields/ Share on other sites More sharing options...
beegro Posted January 13, 2011 Share Posted January 13, 2011 You'll have to hold off on returning a result from the function until all processes are complete. Example: function validate_form(thisform) { var result = true; with (thisform) { if (validate_emailadd(emailadd,"Not a valid e-mail address!")==false) {emailadd.focus;result = false;} if (validate_fname(fname,"Please enter your forename.")==false) {fname.focus();result = false;} if (validate_sname(sname,"Please enter your surname.")==false) {sname.focus();result = false;} return result; } } Quote Link to comment https://forums.phpfreaks.com/topic/224300-validating-multiple-form-fields/#findComment-1158961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.