megz90 Posted April 26, 2008 Share Posted April 26, 2008 hi this JS was created using a previous post. i cant seem to get it working. form allows empty and incorrect values through to the next page. <script type="text/javascript"> function validateForm(form) { var fe = form.elements; //validate horse if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) return false; //validate dob if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) return false; if (!validDob(fe['dbDOB'], 'Date of Birth')) return false; // ueln if (!requiredCheck(fe['dbUELN'], 'UELN')) return false; if (!validPhone(fe['dbUELN'], 'UELN')) return false; return true; } function requiredCheck(fieldObj, fieldName) { if (!fieldObj.value) { alert(fieldName+' is required. Please fill in missing field.'); fieldObj.focus(); return false; } return true; } function validAlpha(fieldObj, fieldName) { validAlphaRegEx = /^[a-zA-Z '-]*$/ if (!fieldObj.value.match(validAlphaRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } function validDob(fieldObj, fieldName) { validDobRegEx = (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2} if (!fieldObj.value.match(validDateRegEx)) { alert(fieldName+' is not a valid date. Please correct and try again.'); fieldObj.focus(); return false; } return true; } function validPhone(fieldObj, fieldName) { validNumberRegEx = /^[\d \-()]*$/ if (!fieldObj.value.match(validNumberRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } </script> start of the form echo " <form method=\"post\" onsubmit=\"return validateForm(this)\" action=\"updatehorse.php\"> "; there are 7 elements in the form, only 3 need to be checked. dbhorsename required any dbDOB date req dd /- mm /- yy dbUELN numbers only im not sure whats going on with it but at the mo anything goes through... thanks Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 26, 2008 Share Posted April 26, 2008 one thing I have had small problems with using functions that return true or false is the leaking of information, you final statement in the function says return true so it will, most of the time try this function validateForm(form) { var fe = form.elements; var answer = true; //validate horse if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) answer = false; //validate dob if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) answer = false; if (!validDob(fe['dbDOB'], 'Date of Birth')) answer = false; // ueln if (!requiredCheck(fe['dbUELN'], 'UELN')) answer = false; if (!validPhone(fe['dbUELN'], 'UELN')) answer = false; return answer; } so it sets up a variable with the value of "true" and changes it if there are any problems and then returns that variable instead of the global true or false Quote Link to comment Share on other sites More sharing options...
megz90 Posted April 26, 2008 Author Share Posted April 26, 2008 thanks for the reply but the code you provided with the top part of mine still did the same thing... is there something that im missing ? the previous code... http://www.phpfreaks.com/forums/index.php/topic,186425.msg835327.html#msg835327 worked fine and when i edited it to work on my other forms it also was alright. it is just on this form im getting problems. <script type="text/javascript"> function validateForm(form) { var fe = form.elements; var answer = true; //validate horse if (!requiredCheck(fe['dbhorsename'], 'Horse Name')) answer = false; //validate dob if (!requiredCheck(fe['dbDOB'], 'Date of Birth')) answer = false; if (!validDob(fe['dbDOB'], 'Date of Birth')) answer = false; // ueln if (!requiredCheck(fe['dbUELN'], 'UELN')) answer = false; if (!validPhone(fe['dbUELN'], 'UELN')) answer = false; return answer; } function requiredCheck(fieldObj, fieldName) { if (!fieldObj.value) { alert(fieldName+' is required. Please fill in missing field.'); fieldObj.focus(); return false; } return true; } function validAlpha(fieldObj, fieldName) { validAlphaRegEx = /^[a-zA-Z '-]*$/ if (!fieldObj.value.match(validAlphaRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } function validDob(fieldObj, fieldName) { validDobRegEx = (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2} if (!fieldObj.value.match(validDateRegEx)) { alert(fieldName+' is not a valid date. Please correct and try again.'); fieldObj.focus(); return false; } return true; } function validPhone(fieldObj, fieldName) { validNumberRegEx = /^[\d \-()]*$/ if (!fieldObj.value.match(validNumberRegEx)) { alert(fieldName+' contains invalid characters.'); fieldObj.focus(); return false; } return true; } </script> Quote Link to comment 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.