jquery.simple.validator
Easy, lightweight and customizable jquery validator.
Description
It's a plugin for jQuery that performs easy client-side validation compatible with all frameworks (PHP, ASP, ASP.Net, Java...). The difference with other validators is that this plugin can perform validation in any HTML container such a form, div, fieldset, table, span, ....
Validations
- Possibility to show error next to field or error summary.
- Possibility to validate on click or autovalidate when leave field (onblur).
- Error CSS customization up to the user.
- Email validator
- IBAN validator
- Required field validator
- Dates validator (with format definition)
- Drowdown element selected validator
- Numbers validator
- Radio elements should be selected validator
- Easy way to create a custom validator
- Validate a group of fields, or just a single field.
External example
Dependencies
jQuery 1.7 or higher
Compatibility
- PHP, JSP, Servlets, Java, Ruby, Python, GoLang...
- Other jQuery plugins
- ASP.Net Web Forms, yes no problem with the single-form-architecture of asp.net because you can use other html containers than
How to include
How to use it
Just use the data tag
standard, that is based on set data-XXXX
where XXXX is the operation you want and data-XXXX-msg
with the message you want.
By default, the validator plugin creates a new span after the control with the error message. When the error is corrected, the control dissapears.
Required field validator
data-required
: Indicates the field is required
data-required-msg
: Error message
Specific error message control
data-message-control
Id of the html control where to show the message. Can be applied in all fields.
Skip validation on a field
data-disable-validation
The control with that tag, will not be validated
Only numbers accepted
data-only-numbers
: The element should only have numbers ( , . - )
data-only-numbers-msg
: Error message
Date format
data-date-format
="dd/MM/yyyy"
data-date-format-separator
="/"
data-date-format-msg
: Error message
Drop down value not selected
data-select-value-different
="0" //You have to select an index different than 0
`data-select-value-different-msg: Error message
IBAN format
data-iban
: Validates IBAN Bank format
data-iban-msg
: Error message
data-email
: Validates valid email entry
data-email-msg
: Error message
Input radio
data-radio-selected
: The radio button should have a value selected or checked
data-radio-selected-msg
: Error message
Methods
$(#container).initialize(options)
Initialize the plugin with the specified options. If no options passed, will use default options.
$(#container).validate()
Start all validations
$(#field_name).validateField()
Validate just the specified field.
$(#container).registerValidator(validator_name,validation_function)
Register a custom made validator
Other helper methods
$(#field_name).isEmail()
: Email format is valid
$.isEmail("string_email")
: Email format is valid
$(#field_name).isEmpty()
: No data introduced in field
$.isEmpty("string to check")
: No data introduced in field
By example
<!-- REQUIRED FIELD --> Required Field:
All validators example
<!-- REQUIRED FIELD -->Required Field:
<!-- EMAIL VALIDATION -->EMAIL:
<!-- ONLY NUMBERS -->Only Numbers (Accepts . , -):
<!-- DATE FORMAT VALIDATOR -->Date format (dd/mm/yyyy):
<!-- select a value --> Select a value First value
<!-- IBAN VALIDATION -->IBAN:
<!-- REQUIRED FIELD with message in specific control -->Required Field with message in specific control:
<!-- REQUIRED FIELD VALIDATION DISABLED -->Required Field with validation disabled:
<!-- Radio -->Radio should be selected: Option 1 Option 2
Configurations
errorClass
(string) Css class of the error message is going to be shown. (by default: validator_error)
scrollToFirstError
(true|false) When errors are processed, scroll automatically the page till the first error. (by default true)
scrollToFirstElementTime
(integer) Speed in milliseconds of ths scroll (by defualt, 1000)
scrollToFirstElementOffset
(integer) Offset in pixels of the scroll, to leave a margin when scrolling (by default 0)
debug
(true|false) Show validation status in developer console (by default false)
dataDynamicRevalidate
(true|false) Autovalidate fields when blur (by default false)
validationSummary
(true|false) Activate if you want to show the errors in a central spot (by default false)
validationSummaryControl
(string) The id of the control where to show the summary
How to set the options:
;
How to define your own custom validators or custom errors
The method registerValidator() receives 2 parameters
- data-tag-validator-name:
- function that performs validation. The functions receives as a parameter a jQuery object with the field that is being validated. Has to return true if validation is OK, or false if there is an error.
Example:
; ; var ok = ;
How to validate just a field
; var ok = ;