Demo
Other examples
Features
- Validate empty fields (including black space)
- Validate email format (RFC822 specification)
- Validate numeric value with optional minimum and maximum value
- Accessibility enhancement
Install
You can get a fresh copy of Baze Validation via Bower.
bower install baze-validation --save
Or you can download it directly from the repository.
Dependencies
Baze Validation depends on 2 libraries:
So make sure to include those 2 libraries before Baze Validation
<script src="jquery.js"></script>
<script src="sprintf.js"></script>
<script src="baze.validation.js"></script>
How to use
Given the markup
<form>
<label for="input1">Username</label>
<input id="input1" type="text" required>
<label for="input2">Email</label>
<input id="input2" type="email" required>
<label for="input3">Age</label>
<input id="input3" type="number" required>
</form>
For quick initialization using default options
$('form').bazeValidate();
Or you can pass options to suit your needs
$('form').bazeValidate({
classInvalid : 'form-input--error',
classValid : 'form-input--success',
classMsg : 'form-msg-error',
msgEmpty : 'This field is required.',
msgEmail : 'Invalid email address.',
msgNumber : 'Input must be number.',
msgExceedMin : 'Minimum number is %s.',
msgExceedMax : 'Maximum number is %s.',
onValidated : function (e) {
// scary code
}
});
Options
Destroy
In case you need to destroy Baze Validation instance
$('form').trigger('bazevalidate.destroy');
How it works
- Attribute novalidate will be added to <form> element to prevent browser's default form validation.
- Baze Validation will validate all elements inside <form> that has required attribute and is not disabled. Elements without required attribute (optional field) will be ignored.
- If a field fail the validation, Baze Validation will add input-invalid class (customizable) and an error message with class msg-error (customizable) will be added after the element.
- If pass, Baze Validation will add input-valid class to the input.
- Baze Validation with fire onValidated callback function when all validation pass if any.
Styling
Baze Validation doesn't offer any default styling if a field is valid or invalid, so the presentation is fully in your control.
Browsers support
- IE8+
- Chrome 14+
- Firefox 10+
- Safari 5+
- Opera 11.1
- iOS 5.1+
- Android 2.2+