Reprinted - jquery validate the use Xiangjie

The default validation rules

(1) required: true losing field
(2) remote: "check.php" using ajax method call check.php validate input values
(3) email: true must enter the correct e-mail format
(4) url: true must enter the correct URL format
(5) date: true must enter the correct date format
(6) dateISO: true must enter the correct date format (ISO), for example :2009-06-23, 1998/01/22 verification only format, do not verify the validity of
(7) number: true must enter a valid number (negative numbers, decimal)
(8) digits: true must enter an integer
(9) creditcard: must enter a valid credit card number
(10) equalTo: "# field" and the # field enter the value must be the same
(11) accept: Input string has a valid suffix name (upload the file suffix)
(12) maxlength: 5 input length at most 5 string (character count as one character)
(13) minlength: 10 Enter the minimum length is 10 string (character count as one character)
(14) rangelength: [5,10] input length must be between 5 and 10 between the string ") (character count as one character)
(15) range: [5,10] input values must be between 5 and 10 between
(16) max: 5 input values can not be greater than 5
(17) min: 10 input values can not be less than 10

Default prompt
messages: (
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
dateDE: "Bitte geben Sie ein g eyebrow ltiges Datum ein.",
number: "Please enter a valid number.",
numberDE: "Bitte geben Sie eine Nummer ein.",
digits: "Please enter only digits",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: $. validator.format ("Please enter no more than (0) characters."),
minlength: $. validator.format ("Please enter at least (0) characters."),
rangelength: $. validator.format ("Please enter a value between (0) and (1) characters long."),
range: $. validator.format ("Please enter a value between (0) and (1 }."),
max: $. validator.format ("Please enter a value less than or equal to (0 }."),
min: $. validator.format ("Please enter a value greater than or equal to (0).")

Such as the need to change, can be added in the js code:

jQuery.extend (jQuery.validator.messages, (
required: "Required field"
remote: "Please correct the field,"
email: "Please enter a valid e-mail format"
url: "Please enter a valid URL"
date: "Please enter a valid date",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number",
digits: "can only enter integer"
creditcard: "Please enter a valid credit card number,"
equalTo: "Please re-enter the same value"
accept: "Please enter a legitimate name suffix string"
maxlength: jQuery.validator.format ("Please enter a maximum of the string is (0)")
minlength: jQuery.validator.format ("Please enter a minimum length of the string is (0)")
rangelength: jQuery.validator.format ("Please enter a range of (0) and (1) string between")
range: jQuery.validator.format ("Please enter a range of (0) and (1) value between")
max: jQuery.validator.format ("Please enter a maximum value of (0)")
min: jQuery.validator.format ("Please enter a minimum value for the (0)")

Recommended practices, this file into messages_cn.js, in the introduction page
<script src="../js/messages_cn.js" type="text/javascript"> </ script>

1. Will check write control rules
<script src="../js/jquery.js" type="text/javascript"> </ script>
<script src="../js/jquery.validate.js" type="text/javascript"> </ script>
<script src="./js/jquery.metadata.js" type="text/javascript"> </ script>

$ (). Ready (function () (
$ ("# SignupForm"). Validate ();

<form method="get" action="">
<label for="firstname"> Firstname </ label>
<input name="firstname" />

<label for="email"> E-Mail </ label>
<input name="email" />

<label for="password"> Password </ label>
<input name="password" type="password" />

<label for="confirm_password"> Confirm Password </ label>
<input name="confirm_password" type="password" />

<input type="submit" value="Submit"/>
</ Form>

Use class ="{}" way to introduce package: jquery.metadata.js

You can use the following method, modify the content tips:
class = "(required: true, minlength: 5, messages: (required: 'Please enter content'))"

Using equalTo keywords, behind the content must be enclosed in quotation marks, the following code:
class = "(required: true, minlength: 5, equalTo: '# password')"

Another way, using keywords: meta ( In order to metadata using other plugins you to package your De validation rules in the projects they Ziji Keyi use this special option)
Tell the validation plugin to look inside a validate-property in metadata for validation rules.
For example:
meta: "validate"
<input name="password" type="password" />

Another way:
$. Metadata.setType ("attr", "validate");

This can validate = "(required: true)" way, or class = "required", but the class = "(required: true, minlength: 5)" will not work

2. To write the code validation rules

$ (). Ready (function () (
$ ("# SignupForm"). Validate ((
rules: (
firstname: "required",
email: (
required: true,
email: true
password: (
required: true,
minlength: 5
confirm_password: (
required: true,
minlength: 5,
equalTo: "# password"
messages: (
firstname: "Please enter the name"
email: (
required: "Please enter the Email address"
email: "Please enter a valid email address"
password: (
required: "Please enter your password"
minlength: jQuery.format ("password can not be less than (0) characters")
confirm_password: (
required: "Please enter the confirmation code"
minlength: "Confirm password can not be less than 5 characters"
equalTo: "Two password inconsistencies inconsistency"

/ / Messages office, if not a control message, will call the default message

<form method="get" action="">
<label for="firstname"> Firstname </ label>
<input name="firstname" />

<label for="email"> E-Mail </ label>
<input name="email" />

<label for="password"> Password </ label>
<input name="password" type="password" />

<label for="confirm_password"> Confirm Password </ label>
<input name="confirm_password" type="password" />

<input type="submit" value="Submit"/>
</ Form>

required: true must have a value
required: "# aa: checked" expression is true, you need to verify
required: function () () return true, the table need to verify the back of two types, the form need to fill or not fill the same time elements

Common methods and attention problems
1. By other means instead of the default SUBMIT

$ (). Ready (function () (
$ ("# SignupForm"). Validate ((
submitHandler: function (form) (
alert ("submitted");
form.submit ();

Validate the default values can be set, written as follows:
$. Validator.setDefaults ((
submitHandler: function (form) (alert ("submitted!"); form.submit ();)

If you want to submit the form, need to use form.submit () instead of using $ (form). Submit ()

2.debug, if this parameter is true, then the form will not submit, just checking, debugging is very convenient when
$ (). Ready (function () (
$ ("# SignupForm"). Validate ((
debug: true
If a page has multiple forms, with
$. Validator.setDefaults ((
debug: true

3.ignore: ignore some elements do not validate
ignore: ". ignore"

4.errorPlacement: Callback Default: the error message on the validation of the elements specified in the error back to the position by default, is: error.appendTo (element.parent ()); that the error message on the validation of the elements behind
errorPlacement: function (error, element) (
error.appendTo (element.parent ());

/ / Example:
<td> <label for="firstname"> First Name </ label> </ td>
<td> <input name="firstname" type="text" value="" maxlength="100" /> </ td>
<td> </ td>
</ Tr>
<input name="dateformat" type="radio" value="0" />
<label for="dateformat_eu"> 14/02/07 </ label>
</ Td>
<input name="dateformat" type="radio" value="1" />
<label for="dateformat_am"> 02/14/07 </ label>
</ Td>
<td> </ td>
</ Tr>
<td> </ td>
<td colspan="2">
<input type="checkbox" name="terms" />
<label for="terms"> I have read and accept the Terms of Use. </ label>
</ Td>
</ Tr>

errorPlacement: function (error, element) (
if ( (": radio"))
error.appendTo (element.parent (). next (). next ());
else if ( (": checkbox"))
error.appendTo ( ());
error.appendTo (element.parent (). next ());

The role of the code is: Under normal circumstances the error message displayed in the <td> </ td>, if a radio show in <td> </ td>, if the content is displayed on the back of checkbox

errorClass: String Default: "error"
Specified error of the css class name, you can customize the style of errors

errorElement: String Default: "label"
What is wrong with the tag, the default is the label you can change em

errorContainer: Selector
Show or hide authentication information, you can automatically have an error message appears when the container property into a show, when no error concealment, use less
errorContainer: "# messageBox1, # messageBox2"

errorLabelContainer: Selector
The error message is unified in a container inside.

wrapper: String
What label wrap then on top of errorELement

General these three properties simultaneously, to achieve in a container display all error functions, and are hidden when no information

errorContainer: "div.error",
errorLabelContainer: $ ("# signupForm div.error"),
wrapper: "li"

Set the error style, can increase the icon

input.error (border: 1px solid red;)
label.error (
background: url (". / demo / images / unchecked.gif") no-repeat 0px 0px;

padding-left: 16px;

padding-bottom: 2px;

font-weight: bold;

color: # EA5200;
label.checked (
background: url (". / demo / images / checked.gif") no-repeat 0px 0px;

success: String, Callback
To verify that the elements of the action through the validation, if with a string, will be treated as a css class can also be a function with
success: function (label) (
/ / Set as text for IE
label.html (""). addClass ("checked");
/ / Label.addClass ("valid"). Text ("Ok!")
Add a "valid" to verify the elements defined in the CSS style <style> label.valid () </ style>
success: "valid"

nsubmit: Boolean Default: true
Verify the time of submission. Set the only false to use other means to verify
onfocusout: Boolean Default: true
Loss of focus is to verify (not including checkboxes / radio buttons)
onkeyup: Boolean Default: true
When the keyup validation.
onclick: Boolean Default: true
Click the checkboxes and radio when the validation
focusInvalid: Boolean Default: true
After the form is submitted, not verified form (submitted prior to the first or the focus of the failed verification form) will be the focus
focusCleanup: Boolean Default: false
If this is true then when the element is not validated when the focus to remove errors. Used together to prevent and focusInvalid

/ / Reset form
$ (). Ready (function () (
var validator = $ ("# signupForm"). validate ((
submitHandler: function (form) (
alert ("submitted");
form.submit ();
$ ("# Reset"). Click (function () (
validator.resetForm ();


remote: URL
Way to use ajax validation, the default will be submitted to the current value to the remote address verification, if submitted to other values, you can use the data options

remote: "check-email.php"

remote: (
url: "check-email.php", / / background processing
type: "post", / / data send mode
dataType: "json", / / accept the data format
data: (/ / data to be passed
username: function () (
return $ ("# username"). val ();

Remote address only the output "true" or "false", can not have other output

addMethod: name, method, message
Custom authentication methods

/ / Two byte characters
jQuery.validator.addMethod ("byteRangeLength", function (value, element, param) (
var length = value.length;
for (var i = 0; i <value.length; i + +) (
if (value.charCodeAt (i)> 127) (
length + +;
return this.optional (element) | | (length> = param [0] & & length <= param [1]);
), $. Validator.format ("Please be sure to enter the value (0) - (1) bytes between (a 2-byte characters count )"));

/ / Postal Code authentication
jQuery.validator.addMethod ("isZipCode", function (value, element) (
var tel = / ^ [0-9] (6) $ /;
return this.optional (element) | | (tel.test (value));
), "Please correctly fill in your zip code");

radio and checkbox, select the authentication

radio's required that one must be selected
<input type="radio" value="m" name="gender" />
<input type="radio" value="f" name="gender"/>

The required checkbox that must be selected
<input type="checkbox" name="agree" />

must select the checkbox of minlength that the minimum number, maxlength that selected the largest number, rangelength: [2,3] that the number of intervals selected
<input type="checkbox" value="email" name="spam[]" />
<input type="checkbox" value="phone" name="spam[]" />
<input type="checkbox" value="mail" name="spam[]" />

select the required selected value that can not be empty
<select name="jungle" title="Please select something!">
<option value=""> </ option>
<option value="1"> Buga </ option>
<option value="2"> Baga </ option>
<option value="3"> Oi </ option>
</ Select>

select the minlength that select the minimum number (can be selected in the select), maxlength that selected the largest number, rangelength: [2,3] that the number of intervals selected
<select name="fruit" title="Please select at least two fruits" multiple="multiple">
<option value="b"> Banana </ option>
<option value="a"> Apple </ option>
<option value="p"> Peach </ option>
<option value="t"> Turtle </ option>
</ Select>

分类:Web 时间:2010-07-15 人气:227
blog comments powered by Disqus


  • Exist to detect the target URL mode e-mail address 2010-11-04

    Exist to detect the target URL mode e-mail address Information disclosure: Information disclosure Low 5 View Vulnerability URL: Descrip

  • struts2 ajax verify the existence of input values 2009-03-07

    struts2 ajax verify the existence of input values Today to do the struts2 ajax validate input regarding the existence of the function, and now share with everyone, I hope everyone many opinions!!! input.jsp page code: <html> <body> <s:form

  • List of form validation rules DWZ 2011-05-24

    <form onsubmit="return validateCallback(this)" action="ajaxDone.html" method="post"> real name: <input type="text" maxlength="20" size="30" name="name"> (required validation

  • Custom form validation rules 2010-11-08

    1. Custom form validation properties :datatype,scriptPrompt,nomorethan,notnull; This is not the html element of all four properties , Custom rule properties, brief introduction : datatype: To verify the data type : scriptPrompt: Authentication does n

  • Credit card reward points credit card not to forget the integral use of large differences 2010-03-03

    Consortium to promote the safety card World Expo Currently, Shanghai has been the number of bank cards issued over 100 million, to pay for improving and optimizing the environment for the World Expo, Shanghai Branch of China Union Pay card-issuing ba

  • summary of input processing input box js [reproduced] 2010-09-26

    javascritp achieve input-related restrictions on use of the input box 1. Cancel button is pressed, the dashed box in added property value in the input or HideFocus = true hideFocus 2. Read-only text box to add the content in property values in the in

  • Action Basic authentication (validation rules file) 2010-10-06

    Jsp client requests Action can not do server-side processing, you can override the execute method can not write, do not write when the default return success The preparation of calibration files (attention and Action in a corresponding directory, the

  • php: / / input mean? php introduction of the input stream input 2010-10-31

    PHP input stream php: / / input Use xml-rpc time, server-side access to client data, mainly through the php input stream input, rather than the $ _POST array. So, here focuses on the input stream php php: / / input On a php: / / input description, PH

  • Close input method input 2009-10-21

    Close input method input method in this text box is closed: Syntax: Example: <input type="text" name="textfield">

  • cakephp learning more than one field validation rules 2010-05-14

    cakephp learning more than one field validation rules Any validation, the data validation is very important; cake for us to package a good framework for approaching the validation rules are commonly used, well, taste it, look below to check 'user' is

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.879 (s). 12 q(s)