-
accept( String extension ) returns Boolean
Makes the element require a certain file extension.
Returns true if the value ends with one of the specified file extensions. If nothing is specified, only images are allowed (png, jpeg, gif).
Works with text inputs.Example:
Makes "field" required and ending with ".xls" or ".csv".
$("#myform").validate({ rules: { field: { required: true, accept: "xls|csv" } } });
HTML:
<form id="myform"> <label for="field">Required, only .xls and .csv files allowed</label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
addMethod( String name, Callback method, String, Function message ) returns undefined
Add a new validation method. It must consist of a name (must be a legal javascript identifier), a function and a default message.
Please note: While the temptation is great to add a regex method that checks it's paramter against the value, it is much cleaner to encapsulate those regular expressions inside their own method. If you need lots of slightly different expressions, try to extract a common parameter.
A library of regular expressions: http://regexlib.com/DisplayPatterns.aspxExample:
Add a validation method that checks if a value starts with a certain domain.
jQuery.validator.addMethod("domain", function(value) { return this.optional(element) || /^http://mycorporatedomain.com/.test(value); }, "Please specify the correct domain for your documents");
Example:
Adds a validation method that checks if a given value equals the addition of the two parameters.
jQuery.validator.addMethod("math", function(value, element, params) { return this.optional(element) || value == params[0] + params[1]; }, String.format("Please enter the correct value for {0} + {1}"));
-
blank( ) returns Array<Element>
Matches elements with a blank value
Blank means either no value at all or only whitespace.
The implementation does a check like this: jQuery.trim(value).length == 0Example:
Finds input elements with no value or just whitespace.
$("input:blank").css("background-color", "#bbbbff");
HTML:
<input value=""/><input value=" "/><input value="abc"/>
-
creditcard( ) returns Boolean
Makes the element require a creditcard number.
Return true, if the value is a valid creditcard number.
Works with text inputs.Example:
Makes "field" required and a creditcard number.
$("#myform").validate({ rules: { field: { required: true, creditcard: true } } });
HTML:
<form id="myform"> <label for="field">Required, creditcard: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
date( ) returns Boolean
Makes the element require a date.
Return true, if the value is a valid date. Uses JavaScripts built-in Date to test if the date is valid, and is therefore very limited.
Works with text inputs.Example:
Makes "field" required and a date.
$("#myform").validate({ rules: { field: { required: true, date: true } } });
HTML:
<form id="myform"> <label for="field">Required, date: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
dateDE( ) returns Boolean
Makes the element require a german date.
Return true, if the value is a valid date. Supports german dates (29.04.1994 or 1.1.2006). Doesn't make any sanity checks.
Works with text inputs.Example:
Makes "field" required and a german date.
$("#myform").validate({ rules: { field: { required: true, dateDE: true } } });
HTML:
<form id="myform"> <label for="field">Required, date: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
dateISO( ) returns Boolean
Makes the element require a ISO date.
Return true, if the value is a valid date, according to ISO date standard.
Works with text inputs.Example:
Makes "field" required and a ISO date.
$("#myform").validate({ rules: { field: { required: true, dateISO: true } } });
HTML:
<form id="myform"> <label for="field">Required, dateISO: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
digits( ) returns Boolean
Makes the element require digits only.
Returns true if the value contains only digits.
Works with text inputs.Example:
Makes "field" required and digits only.
$("#myform").validate({ rules: { field: { required: true, digits: true } } });
HTML:
<form id="myform"> <label for="field">Required, digits: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
element( Selector element ) returns Boolean
Validates a single element, returns true if it is valid, false otherwise.
This behaves as validation on blur or keyup, but returns the result.Example:
Triggers element validation programmatically.
$("#myform").validate().element( "#myselect" );
-
email( ) returns Boolean
Makes the element require a valid email
Return true, if the value is a valid email address.
Works with text inputs.Example:
Makes "field" required and an email address.
$("#myform").validate({ rules: { field: { required: true, email: true } } });
HTML:
<form id="myform"> <label for="field">Required, email: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
equalTo( Selector other ) returns Boolean
Requires the element to be the same as another one
Returns true if the value has the same value as the element specified by the first parameter.
Works with text inputs.Example:
Makes "field" required to be the same as #other
$("#myform").validate({ rules: { password: "required", "password_again": { equalTo: "#password" } } });
HTML:
<form id="myform"> <label for="password">Password</label> <input id="password" name="password" /> <br/> <label for="password_again">Again</label> <input class="left" id="password_again" name="password_again" /> <br/> <input class="submit" type="submit" value="Validate!" /> </form>
-
filled( ) returns Array<Element>
Matches elements with a value.
filled means any value, but not only whitespace.
The implementation does a check like this: jQuery.trim(value).length > 0Example:
Finds input elements with no value or just whitespace.
$("input:filled").css("background-color", "#bbbbff");
HTML:
<input value=""/><input value=" "/><input value="abc"/>
-
form( ) returns Boolean
Validates the form, returns true if it is valid, false otherwise.
This behaves as a normal submit event, but returns the result.Example:
Triggers form validation programmatically.
$("#myform").validate().form()
-
maxLength( Integer length ) returns Boolean
Makes the element require a given maxmimum length.
Return false, if the element is * some kind of text input and its value is too long * a set of checkboxes has too many boxes checked * a select and has too many options selected
Works with text inputs, selects and checkboxes.Example:
Makes "field" required having at most 4 characters.
$("#myform").validate({ rules: { field: { required: true, maxLength: 4 } } });
HTML:
<form id="myform"> <label for="field">Required, maximum length 4: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
maxValue( Integer value ) returns Boolean
Makes the element require a given maximum value.
Works with text inputs.Example:
Makes "field" required and 23 or smaller.
$("#myform").validate({ rules: { field: { required: true, maxValue: 23 } } });
HTML:
<form id="myform"> <label for="field">Required, maximum value 23: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
minLength( Integer length ) returns Boolean
Makes the element require a given minimum length.
Return false, if the element is * some kind of text input and its value is too short * a set of checkboxes has not enough boxes checked * a select and has not enough options selected
Works with text inputs, selects and checkboxes.Example:
Makes "field" required having at least 3 characters.
$("#myform").validate({ rules: { field: { required: true, minLength: 3 } } });
HTML:
<form id="myform"> <label for="field">Required, Minimum length 3: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
minValue( Integer value ) returns Boolean
Makes the element require a given minimum value.
Works with text inputs.Example:
Makes "field" required and 13 or larger.
$("#myform").validate({ rules: { field: { required: true, minValue: 13 } } });
HTML:
<form id="myform"> <label for="field">Required, minimum value 13: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
rangeLength( Array<Integer> range ) returns Boolean
Makes the element require a given value range.
Return false, if the element is * some kind of text input and its length is too short or too long * a set of checkboxes has not enough or too many boxes checked * a select and has not enough or too many options selected
Works with text inputs.Example:
Makes "field" required and between 2 and 6 characters long.
$("#myform").validate({ rules: { field: { required: true, rangeLength: [2, 6] } } });
HTML:
<form id="myform"> <label for="field">Required, minium length 2, maximum length 6: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
rangeValue( Array<Integer> range ) returns Boolean
Makes the element require a given value range.
Works with text inputs.Example:
Makes "field" required and between 13 and 23.
$("#myform").validate({ rules: { field: { required: true, rangeValue: [13, 23] } } });
HTML:
<form id="myform"> <label for="field">Required, minium 13, maximum 23: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
refresh( Selector selection ) returns undefined
Call to refresh a form after new elements have been added or rules changed.
Accepts an optional argument to refresh only a part of the form, eg. only the newly added element.Example:
Refrehes the validator after the form has changed.
var validator = $("#myform").validate(); validator.refresh();
Example:
Refreshes the validator after a new element has been added.
var validator = $("#myform").validate(); validator.refresh( "#newElement" );
-
required( ) returns Boolean
Makes the element always required.
Return false if the element is empty (text input) or unchecked (radio/checkbxo) or nothing selected (select).
Works with text inputs, selects, checkboxes and radio buttons.
To force a user to select an option from a select box, provide an empty options like <option value="">Choose...</option>Example:
Makes "field" always required. Nothing and blanks are invalid.
$("#myform").validate({ rules: { field: "required" } });
HTML:
<form id="myform"> <label for="field">Required: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
Example:
Makes the fruit select required.
$("#myform").validate({ rules: { fruit: "required" } });
HTML:
<form id="myform"> <label for="fruit">Please select a fruit</label> <select id="fruit" name="fruit" title="Please select something!" <option value=""></option> <option value="1">Banana</option> <option value="2">Apple</option> <option value="3">Peach</option> </select> <br/> <input type="submit" value="Validate!" /> </form>
Example:
Makes the gender radio buttons required.
$("#myform").validate({ rules: { gender: "required" } });
HTML:
<form id="myform"> <label for="gender_male"> <input type="radio" id="gender_male" value="m" name="gender" /> Male </label> <label for="gender_female"> <input type="radio" id="gender_female" value="f" name="gender"/> Female </label> <label for="gender" class="error">Please select your gender</label> <br/> <input type="submit" value="Validate!" /> </form>
Example:
Makes the agree checkbox required.
$("#myform").validate({ rules: { agree: "required" } });
HTML:
<form id="myform"> <label for="agree">Please agree to our policy</label> <input type="checkbox" id="agree" title="Please agree to our policy!" name="agree" /> <br/> <input type="submit" value="Validate!" /> </form>
-
required( String dependency-expression ) returns Boolean
Makes the element required, depending on the result of the given expression.
Return false if the element is empty (text input) or unchecked (radio/checkbxo) or nothing selected (select).
Works with all kind of text inputs, selects, checkboxes and radio buttons.
To force a user to select an option from a select box, provide an empty options like <option value="">Choose...</option>Example:
Makes "newsletter-type" required only if "newsletter" is checked.
$("#myform").validate({ rules: { "newsletter-type": { required: "#newletter:checked" } } });
-
required( Callback dependency-callback ) returns Boolean
Makes the element required, depending on the result of the given callback.
Return false if the element is empty (text input) or unchecked (radio/checkbxo) or nothing selected (select).
Works with all kind of text inputs, selects, checkboxes and radio buttons.
To force a user to select an option from a select box, provide an empty options like <option value="">Choose...</option>Example:
Makes "parent" required only if age is below 13.
$("#myform").validate({ rules: { parent: { required: function(element) { return $("#age").val() < 13; } } } });
-
resetForm( ) returns undefined
Resets the controlled form.
Resets input fields to their original value (requires form plugin), removes classes indicating invalid elements and hides error messages.Example:
Reset the form controlled by this validator.
var validator = $("#myform").validate(); validator.resetForm();
-
setDefaults( Options defaults ) returns undefined
Modify default settings for validation.
Accepts everything that <a href='Plugins/Validation/validate'>Plugins/Validation/validate</a> accepts.Example:
Sets the debug setting for all validation calls.
jQuery.validator.setDefaults({ debug: true );
-
showErrors( Object<String, String> errors ) returns undefined
Show the specified messages.
Keys have to refer to the names of elements, values are displayed for those elements, using the configured error placement.Example:
Adds and shows error message programmatically.
var validator = $("#myform").validate(); validator.showErrors({"firstname": "I know that your firstname is Pete, Pete!"});
-
treeview( Options options ) returns jQuery
Takes an unordered list and makes all branches collapsable.
The "treeview" class is added if not already present.
To hide branches on first display, mark their li elements with the class "closed". If the "collapsed" option is used, mark initially open branches with class "open".
Options
Example:
Creates a treeview and adds a button to add more branches to it.
$("#browser").treeview(); $("#add").click(function() { var branches = $("<li><span class='folder'>New Sublist</span><ul>" + "<li><span class='file'>Item1</span></li>" + "<li><span class='file'>Item2</span></li>" + "</ul></li>").appendTo("#browser"); $("#browser").treeview({ add: branches }); });
HTML:
<ul id="browser" class="filetree"> <li><span class="folder">Folder 1</span> <ul> <li><span class="file">Item 1.1</span></li> </ul> </li> <li><span class="folder">Folder 2</span> <ul> <li><span class="folder">Subfolder 2.1</span> <ul id="folder21"> <li><span class="file">File 2.1.1</span></li> <li><span class="file">File 2.1.2</span></li> </ul> </li> <li><span class="file">File 2.2</span></li> </ul> </li> <li class="closed"><span class="folder">Folder 3 (closed at start)</span> <ul> <li><span class="file">File 3.1</span></li> </ul> </li> <li><span class="file">File 4</span></li> </ul> <button id="add">Add!</button>
-
unchecked( ) returns Array<Element>
Matches all elements that are unchecked.
Inversion of <a href='Selectors/checked'>:checked</a>.Example:
Finds all input elements that are unchecked.
function countUnchecked() { var n = $("input:unchecked").length; $("div").text(n + (n == 1 ? " is" : " are") + " unchecked!"); } countUnchecked(); $(":checkbox").click(countUnchecked);
HTML:
<form> <input type="checkbox" name="newsletter" checked="checked" value="Hourly" /> <input type="checkbox" name="newsletter" value="Daily" /> <input type="checkbox" name="newsletter" value="Weekly" /> <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> <input type="checkbox" name="newsletter" value="Yearly" /> </form> <div></div>
Result:
[ <input type="checkbox" name="newsletter" checked="checked" value="Daily" />, <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ]
-
url( ) returns Boolean
Makes the element require a valid url
Return true, if the value is a valid url.
Works with text inputs.Example:
Makes "field" required and a url.
$("#myform").validate({ rules: { field: { required: true, url: true } } });
HTML:
<form id="myform"> <label for="field">Required, URL: </label> <input class="left" id="field" name="field" /> <br/> <input type="submit" value="Validate!" /> </form>
-
valid( ) returns Boolean
Validates the selected form or elements.
<a href='Plugins/Validation/validate'>validate()</a> needs to be called on the form before checking it using this method. Shortcut for the validator API.Example:
Sets up validation for a form, then checks if the form is valid.
$("#myform").validate(); // this could be called after some user interaction $("#myform").valid()
-
validate( Options options ) returns [[Plugins/Validation#Validator|Validator]]
Validates the selected form.
This method adds, by default, a submit-event-handler to the form and keyup and blur handlers to each element within the form.
Validation rules can be specified via the rules-option or via metadata (requires metadata plugin). Messages can be left out (there is a default message for each method), specified via the messages-option or via the title-attribute on each element.Options