jQuery Validation 1.9.0
Evaluation of jQuery Validation
Requirements
- Create the validation object in JS
initializeValidation({ form: $('#frmMaint'), errorClass: "formError", rules:"", messages:""});
- Incorporate class' on the form itself to define how you validate the form
- Incorporate validation attributes to the form
- Add custom validation to the page via javascript
Documentation
Define Validation Object
JS
initializeValidation();
Your validation object can take many many arguments and rules.
JS
initializeValidation({
form:$("#frmMaint"),
errorClass:"formError",
rules:"'EffectiveDate': { required: true },'Zip': {required: true, range: [00001, 99999]}, 'BirthDate': { age: true, mmddyyyy: true}",
messages:"id: 'Custom default error message'"
});
Class Methods
Add these classes to your form input's.
For example class="required date"
would validate that element as a date, and it's a required field.
- required
- url
- date
- dateISO
- number
- digits
- creditcard
Attributes
These attributes can be used in validation in a form element, but outside of the class attribute definition.
For example, class="required" min="10"
would validate a required object, that demands a minimum of 10 characters
JS
accept="xml|css"
equalTo="#ID"
min="10"
max="100"
maxlength="3"
minlength="55"
Custom Validation
To create a custom validation class
JS
//Test a regular expression
$.validator.addMethod("mmddyyyy", function (value, element) {
return this.optional(element) || /^((((0[13578])|(1[02]))[\/]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\/]?(([0-2][0-9])|(30)))|(02[\/]?[0-2][0-9]))[\/]?\d{4}$/.test(value);
}, "This must be a valid MM/DD/YYYY date.");
//Test conditional statements, return true or false.
$.validator.addMethod("age", function (value, element) {
birthdayDate = new Date(value);
dateNow = new Date();
var years = dateNow.getFullYear() - birthdayDate.getFullYear();
if (years >= 65) { return false; }
else { return true; }
}, "You must be younger than 65 years old");
Extras
To validate the form
JS
$('#frmMaint').valid();
To validate a single field
JS
$('#id').valid();