i have form requires input data validated before submission. types of validation checks - can more 1 per input element - entered input element, , validation performed jquery.
my approach put types of validation required , separated commas - such "blank,email" - "checks" attribute of input element. jquery extract part, split array, , loop through each validation type.
the problem is, not want submit form until validation checks completed. when use "split" function, returns array, program takes boolean true returned , form submitted after first input element.
here's sample of form, taking note looks similar other fields:
<tr> <td>full name</td> <td><input type="text" name="accountname" check="blank"></td> </tr> <tr> <td>nric</td> <td><input type="text" name="accountnric" check="blank,nric" maxlength="9"></td> </tr>
and here's jquery validation code:
// perform validation checks $("form").submit(function() { // loop through input fields $("form input, form textarea, form select").each(function() { // if none of input's parents hidden if($(this).parents(":hidden").length == 0) { var thisvalue = $(this).val(); var checkwhat = $(this).attr("check"); var checkarray = checkwhat.split(","); for(var i=0; i<checkarray.length; i++) { switch(checkarray[i]) { case "blank": alert(checkblank(thisvalue)); break; case "nric": alert(validatenric(thisvalue)); break; case "email": alert(validateemail(thisvalue)); break; } } } }); });
i should clarify error checking variable hasn't been added in yet codes still being tested in parts.
any appreciated!
// perform validation checks $("form").submit(function() { // loop through input fields $("form input, form textarea, form select").each(function(e) { // if none of input's parents hidden if($(this).parents(":hidden").length == 0) { var _haserror = false; var thisvalue = $(this).val(); var checkwhat = $(this).attr("check"); var checkarray = checkwhat.split(","); for(var i=0; i<checkarray.length; i++) { switch(checkarray[i]) { case "blank": if (!checkblank(thisvalue)) { _haserror = true; } ; break; case "nric": if (!validatenric(thisvalue)) { _haserror = true; }; break; case "email": if (!validateemail(thisvalue)) { _haserror = true; }; break; } } if (_haserror) e.preventdefault(); } }); });
Comments
Post a Comment