jquery form validation, jquery.validate all captures

1, preparations to JQuery version: 1.2.6 +, Compatibility 1.3.2

Official website address: http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Latest version: 1.5.5
Local Download: jquery.validate.zip
Second, the default validation rules

(1)required:true  Losing field
(2)remote:"check.php"  Use AJAX method calls  check.php Verify that the input values
(3)email:true  You must enter a properly formatted email
(4)url:true  You must enter a properly formatted URL
(5)date:true  You must enter a properly formatted date
(6)dateISO:true  You must enter a properly formatted date  (ISO), For example,  :2009-06-23,1998/01/22  Only verifies that the format, does not verify the validity
(7)number:true  You must enter a valid number  ( A negative number, the decimal  )
(8)digits:true  You must enter an integer
(9)creditcard:  You must enter a valid credit card number
(10)equalTo:"#field"  The input value must and  #field The same
(11)accept:  Enter the name of the owning legal suffix string  ( Upload file suffix  )
(12)maxlength:5  Enter up to five string  ( Chinese character is a character  )
(13)minlength:10  Enter the length of the minimum is 10 string  ( Chinese character is a character  )
(14)rangelength:[5,10]  Enter the length must be between 5 and   10  The string between  ")( Chinese character is a character  )
(15)range:[5,10]  The input value must be between 5 and   10  Between
(16)max:5  The input value cannot be greater than the  5
(17)min:10  The input value cannot be less than  10

Third, the 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 Mei  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 fields  ",
        remote: " Please correct the fields  ",
        email: " Please enter a properly formatted email  ",
        url: " Please enter a valid Web address  ",
        date: " Please enter a valid date  ",
        dateISO: " Please enter a valid date   (ISO).",
        number: " Please enter a valid number  ",
        digits: " You can only enter integer  ",
        creditcard: " Please enter a valid credit card number  ",
        equalTo: " Please enter the same value again  ",
        accept: " Please enter the name of the owning legal suffix string  ",
        maxlength: jQuery.validator.format(" Please enter a maximum length   {0}  String  "),
        minlength: jQuery.validator.format(" Please enter a length of at least   {0}  String  "),
        rangelength: jQuery.validator.format(" Please enter a length between   {0}  And   {1}  The string between  "),
        range: jQuery.validator.format(" Please enter a number between   {0}  And   {1}  The values between  "),
        max: jQuery.validator.format(" Please enter a maximum of   {0}  The value of the  "),
        min: jQuery.validator.format(" Please enter a minimum   {0}  The value of the  ")
});

Recommended practices, this file into messages_cn.js, in the introduction page

4, using the method
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="">
<p>
<label for="firstname">Firstname</label>
<input name="firstname" />
</p>
<p>
<label for="email">E-Mail</label>
<input name="email" />
</p>
<p>
<label for="password">Password</label>
<input name="password" type="password" />
</p>
<p>
<label for="confirm_password"> Confirm password  </label>
<input name="confirm_password" type="password" />
</p>
<p>
<input type="submit" value="Submit"/>
</p>
</form>

Use class ="{}" way to introduce package: jquery.metadata.js can use the following method, modify the prompt content:
class = "(required: true, minlength: 5, messages: (required: 'Please enter content'))" in the use equalTo keywords, behind the content must be enclosed in quotation marks, the following code:
class = "(required: true, minlength: 5, equalTo: '# password')" in another way, using the keyword: meta (meta-data to use other plug-ins you want to wrap your validation rules in their own projects can be 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"); so you can use the 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="">
<p>
<label for="firstname"> Firstname </ label>
<input name="firstname" />
</ P>
<p>
<label for="email"> E-Mail </ label>
<input name="email" />
</ P>
<p>
<label for="password"> Password </ label>
<input name="password" type="password" />
</ P>
<p>
<label for="confirm_password"> Confirm Password </ label>
<input name="confirm_password" type="password" />
</ P>
<p>
<input type="submit" value="Submit"/>
</ P>
</ 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 of five commonly used 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:
<tr>
<td> <label for="firstname"> First Name </ label> </ td>
<td> <input name="firstname" type="text" value="" maxlength="100" /> </ td>
<td> </ td>
</ Tr>
<tr>
<td>
<input name="dateformat" type="radio" value="0" />
<label for="dateformat_eu"> 14/02/07 </ label>
</ Td>
<td>
<input name="dateformat" type="radio" value="1" />
<label for="dateformat_am"> 02/14/07 </ label>
</ Td>
<td> </ td>
</ Tr>
<tr>
<td> </ td>
<td colspan="2">
<div>
<input type="checkbox" name="terms" />
<label for="terms"> I have read and accept the Terms of Use. </ label>
</ Div>
</ Td>
</ Tr> errorPlacement: function (error, element) (
if (element.is (": radio"))
error.appendTo (element.parent (). next (). next ());
else if (element.is (": checkbox"))
error.appendTo (element.next ());
else
error.appendTo (element.parent (). next ());
) Function code is: Under normal circumstances the error message displayed in, if that radio displayed, if a checkbox displayed in the content behind the errorClass: String Default: "error"
Specified error of the css class name, you can customize the error style errorElement: String Default: "label"
What is wrong with the tag, the default is the label you can change emerrorContainer: 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 on top of errorELement then wrap the three properties generally be used to achieve in a container show all errors function, and no information automatically hide errorContainer: "div.error",
errorLabelContainer: $ ("# signupForm div.error"),
wrapper: "li" error setting the style, can increase the icon shows 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
success: "valid" nsubmit: Boolean Default: true
Verify the time of submission. Set the only false to use other methods 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. Avoid focusInvalid together with the / / 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 for 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 required verification that radio must select a
<input type="radio" value="m" name="gender" />
<input type="radio" value="f" name="gender"/> checkbox's required that must be selected
<input type="checkbox" name="agree" /> checkbox of minlength expressed the need to select 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-06-11 人气:340
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

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