Baze Validation is a library to run basic form validation
- Validate empty fields (including blank space)
- Validate email format (RFC822 specification)
- Validate numeric value with optional minimum and maximum value
- Accessibility enhancement
You can get a fresh copy of Baze Validation via bower.
bower install baze-validation --save
or you can download the zip file.
Baze Validation depend on jQuery so make sure to include jQuery before Baze Validation
<script src="jquery.js"></script>
<script src="baze.validation.js"></script>
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 : 'input-invalid',
classValid : 'input-valid',
classMsg : '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.',
onValidating : null,
onValidated : null
});
- Type: String
- Default: input-invalid
- Description: Class to be added to invalid input
- Type: String
- Default: input-valid
- Description: Class to be added to valid input
- Type: String
- Default: msg-error
- Description: Class to be added to form error message
- Type: String
- Default: This field is required.
- Description: Text to display if input is empty
- Type: String
- Default: Invalid email address.
- Description: Text to display for invalid email address
- Type: String
- Default: Input must be number.
- Description: Text to display if input value is not numeric
- Type: String
- Default: Minimum number is %s.
- Description: Text to display if input type number's value is less than minimum value specified by min attribute. %s is a placeholder to display the minimum value, so you can place it anywhere or even remove it.
- Type: String
- Default: Maximum number is %s.
- Description: Text to display if input type number's value is greater than maximum value specified by max attribute. %s is a placeholder to display the maximum value, so you can place it anywhere or even remove it.
- Type: Function
- Default: null
- Description: a callback function that fired after the form is validated whether it is valid or invalid.
- Type: Function
- Default: null
- Description: a callback function that fired after the form is valid.
To destroy Baze Validation instance
$('form').trigger('bazevalidate.destroy');
- Attribute
novalidate
will be added to<form>
to prevent browser's default form validation. - Baze Validation will validate all elements inside
<form>
that hasrequired
attribute and is not disabled. Elements withoutrequired
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 classmsg-error
(customizable) will be added after the element. - If pass, Baze Validation will add
input-valid
to the input. - Baze Validation will fire onValidating callback function if specified.
- Baze Validation will fire onValidated callback function when all validation pass if specified.
Baze Validation doesn't offer any default styling if a field is valid or invalid, so the presentation is fully in your control.
- IE8+
- Chrome 14+
- Firefox 10+
- Safari 5+
- Opera 11.1
- iOS 5.1+
- Android 2.2+