JQuery Validation form verification example

Source: Internet
Author: User
Tags valid valid email address

Jq code

The code is as follows: Copy code
/**
* @ Author ming
*/
$ (Document). ready (function (){

/* Set the default attribute */
$. Validator. setDefaults ({
SubmitHandler: function (form ){
Form. submit ();
}
});

// Character verification
JQuery. validator. addMethod ("stringCheck", function (value, element ){
Return this. optional (element) |/^ [u0391-uFFE5w] + $/. test (value );
}, "Can only contain Chinese characters, English letters, numbers, and underscores ");

// Two Chinese 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]);
}, "Please make sure that the input value is between 3-15 bytes (one text is counted as 2 bytes )");

// ID card number verification
JQuery. validator. addMethod ("isIdCardNo", function (value, element ){
Return this. optional (element) | isIdCardNo (value );
}, "Enter your ID card number correctly ");

// Mobile phone number verification
JQuery. validator. addMethod ("isMobile", function (value, element ){
Var length = value. length;
Var mobile =/^ (13 [0-9] {1}) | (15 [0-9] {1}) + d {8}) $ /;
Return this. optional (element) | (length = 11 & mobile. test (value ));
}, "Please enter your mobile phone number correctly ");

// Phone number verification
JQuery. validator. addMethod ("isTel", function (value, element ){
Var tel =/^ d {3, 4 }-? D {7, 9} $/; // phone number format: 010-12345678
Return this. optional (element) | (tel. test (value ));
}, "Please enter your phone number correctly ");

// Contact number (mobile phone/phone) verification
JQuery. validator. addMethod ("isPhone", function (value, element ){
Var length = value. length;
Var mobile =/^ (13 [0-9] {1}) | (15 [0-9] {1}) + d {8}) $ /;
Var tel =/^ d {3, 4 }-? D {7, 9} $ /;
Return this. optional (element) | (tel. test (value) | mobile. test (value ));

}, "Please enter your contact number correctly ");

// Postal code verification
JQuery. validator. addMethod ("isZipCode", function (value, element ){
Var tel =/^ [0-9] {6} $ /;
Return this. optional (element) | (tel. test (value ));
}, "Please enter your zip code correctly ");

// Start verification
$ ('# SubmitForm'). validate ({
/* Set verification rules */
Rules :{
Username :{
Required: true,
StringCheck: true,
ByteRangeLength: [3, 15]
},
Email :{
Required: true,
Email: true
},
Phone :{
Required: true,
IsPhone: true
},
Address :{
Required: true,
StringCheck: true,
ByteRangeLength: [3,100]
 }
},

/* Set the error message */
Messages :{
Username :{
Required: "Enter the user name ",
StringCheck: "The user name can only contain Chinese characters, English letters, numbers, and underscores ",
ByteRangeLength: "The user name must be 3-15 characters long (one Chinese character is counted as 2 characters )"
},
Email :{
Required: "enter an Email address ",
Email: "enter a valid Email address"
},
Phone :{
Required: "Enter your contact number ",
IsPhone: "enter a valid contact number"
},
Address :{
Required: "Enter your contact address ",
StringCheck: "Please enter your contact address correctly ",
ByteRangeLength: "Please provide your contact address so that we can contact you"
 }
},

/* Set the verification trigger event */
FocusInvalid: false,
Onkeyup: false,

/* Set the error message prompt DOM */
ErrorPlacement: function (error, element ){
Error. appendTo (element. parent ());
},

});

});




Test page index.html

The code is as follows: Copy code
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/>
<Title> jQuery verification </title>
<Script src = "lib/jquery/jquery-1.3.2.min.js"> </script>
<Script type = "text/javascript" src = "lib/jquery. validate. js" mce_src = "lib/jquery. validate. js"> </script>
<Script type = "text/javascript" src = "lib/jquery/messages_cn.js"> </script>
<Script type = "text/javascript" src = "lib/jquery/formValidatorClass. js"> </script>
<Style type = "text/css">

*{
Font-family: Verdana;
Font-size: 96%;
 }
Label {
Width: 10em;
Float: left;
 }
Label. error {
Float: none;
Color: red;
Padding-left:. 5em;
Vertical-align: top;
 }
P {
Clear: both;
 }
. Submit {
Margin-left: 12em;
 }
Em {
Font-weight: bold;
Padding-right: 1em;
Vertical-align: top;
 }
</Style>
</Head>
<Body>
<Form class = "submitForm" id = "submitForm" method = "get" action = "">
<Fieldset>
<Legend> form verification </legend>
<P>
<Label for = "username"> user name </label>
<Em> * </em> <input id = "userName" name = "username" size = "25"/>
</P>
<P>
<Label for = "email"> email </label>
<Em> * </em> <input id = "email" name = "email" size = "25"/>
</P>
<P>
<Label for = "phone"> Contact Number </label>
<Em> * </em> <input id = "phone" name = "phone" size = "25" value = ""/>
</P>
<P>
<Label for = "address"> address </label>
<Em> * </em> <input id = "address" name = "address" size = "22">
</P>
<Input class = "submit" type = "submit" value = "submit"/>
</P>
</Fieldset>
</Form>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.