Ads

Tuesday, 19 August 2014

Validate.js - Lightweight JavaScript form validation library

Download Demo


validate.js is a lightweight JavaScript form validation library inspired by CodeIgniter. No dependencies, just over 2kb gzipped, and customizable!

Features


Validate form fields from over a dozen rules

No dependencies

Customizable messages

Supply your own validation callbacks for custom rules

Chainable customization methods for ease of declaration

Conditionally validate certain form fields

Works in all major browsers (even IE6!)

Modeled off the CodeIgniter form validation API

1. INCLUDE JS FILES


<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js”></script>

<script type=”text/javascript” src=”validate.min.js”></script>


2. HTML


<form name=”example_form” action=”#” method=”POST”>

<label for=”req”>Required field:</label>

<label for=”alphanumeric”>Alphanumeric field:</label>


<input name=”req” id=”req”>

<input name=”alphanumeric” id=”alphanumeric”>


<label for=”password”>Password:</label>

<label for=”password_confirm”>Password Confirmation (match password):</label>


<input name=”password” id=”password” type=”password”>

<input name=”password_confirm” id=”password_confirm” type=”password”>


<label for=”email”>Email:</label>

<label for=”minlength”>Min length field (min. 8 chars):</label>


<input name=”email” id=”email”>

<input name=”minlength” id=”minlength”>


<label for=”tos_checkbox”>Required checkbox (example: Terms of Service)</label>

<input name=”tos_checkbox” id=”tos_checkbox” type=”checkbox”>


<button class=”button gray” type=”submit” name=”submit”>Submit</button>

</form>


3. JAVASCRIPT


new FormValidator(‘example_form’, [

name: 'req',

display: 'required',

rules: 'required'

,

name: 'alphanumeric',

rules: 'alpha_numeric'

,

name: 'password',

rules: 'required'

, matches[password]‘

,

name: ‘email’,

rules: ‘valid_email’

,

name: ‘minlength’,

display: ‘min length’,

rules: ‘min_length[8]‘

,

name: ‘tos_checkbox’,

display: ‘terms of service’,

rules: ‘required’

], function(errors, evt)

if (errors.length > 0)

// Show the errors


);


 


new FormValidator(formName, fields, callback)


The FormValidator object is attached to the window upon loading validate.js. After creation, it will validate thefields on submission of the form named formName.


The formName passed in to validate must be the exact value of the name attribute of the form


<form name=”example_form”></form>


An array of fields will be used to perform validation on submission of the form. The array must contain objects containing these properties:


name (required) – The name attribute of the element.


<input name=”email” />


display (optional) – The name of the field as it appears in error messages. If not present in the object, thename parameter will be used.


Example: If your field name is user, you may choose to use a display of “Username.”


rules (required) – One or more rules, which are piped together.


Example – required|min_length[8]|matches[password_confirm]


depends (optional) – A function that runs before the field is validated. If the function returns false, the field is never validated against the declared rules.


depends: function(field)

return Math.random() > .5;


A callback will always be executed after validation. Your callback should be ready to accept two parameters.


errors – An array of errors from the validation object. If the length > 0, the form failed validation


This array will contain javascript objects with up to three properties:

- id: The id attribute of the form element

- name: The name attribute of the form element

- message: The error message to display

- rule: The rule that prompted this error

event – If the browser supports it, the onsubmit event is passed in.


function(errors, event)

if (errors.length > 0)

var errorString = ”;


for (var i = 0, errorLength = errors.length; i < errorLength; i++)

errorString += errors[i].message + ‘<br />’;


el.innerHTML = errorString;



Validate.js - Lightweight JavaScript form validation library

No comments:

Post a Comment