Programming > Web > jQuery Validator
This code will validate any text or password input element with the class required. Anything not accepted will be given the class specified.
function checkRequiredFields( errorClass ) { var continueForm = true; if( errorClass == 'undefined' ) errorClass = "inputError"; $(".required").each(function () { if( !/^[a-zA-Z0-9]+/.test( $(this).val() ) ) { $(this).addClass( errorClass ); continueForm = false; } else { if( $(this).hasClass( errorClass ) ) { $(this).removeClass( errorClass ); } } }); return continueForm; }
After this code is added to a style, make sure to add the following line in your form tag.
<form onsubmit="return checkRequiredFields();"> <input type="text" name="testField" class="required" /> </form>