I have some code that tests a couiple of things on my form. Now I have added a catpcha field and the value required to pass will always be 4. So, the surfer has to enter 4 into the catpcha field. How do I modify the following code to do this? The catpcha field is called catpcha. <script language="JavaScript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value==""||value=="Select") { alert(alerttxt);return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false;} else if (validate_required(country,"Country must be filled out!")==false) {country.focus();return false;} } } </script> Code (markup): Thanks, Jon
You should always do such kind of validations also on your server side. <script language="JavaScript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value==""||value=="Select") { alert(alerttxt);return false; } else { return true; } } } function validate_equals(field,fieldValue,alerttxt) { with (field) { if (value==null||value==""||value!= fieldValue) { alert(alerttxt);return false; } else { return true; } } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false;} else if (validate_required(country,"Country must be filled out!")==false) {country.focus();return false;} else if (validate_equals(catpcha,4,"Incorrect catpcha value!") == false) {catpcha.focus(); return false;} } } </script> PHP:
To validate forms why not use jquery? Its so easy! check this pretty good tutorial: http://corpocrat.com/2009/07/15/quick-easy-form-validation-tutorial-with-jquery/
Serverside validation is essential but having a user friendly prompt before submission is always nice.