JQuery Validate extensions set error messages via parameters

Source: Internet
Author: User
Tags min

The errors that eventually appear on the page fall into two categories: the first is the default error message, which is already defined in the plugin and can be manually modified

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>

<script src= "Jquery-1.5.js" type= "text/Web Effects" ></script>

<script src= "Jquery.validate.min.js" type= "Text/javascript" ></script>

    <script type= "Text/javascript"
        JQuery.validator.addMethod ("regex", //addmethod 1th parameter: Method name
        function (value, element, params) {    //addmethod 2nd Parameter: Authentication method,
            //Validation method parameters (values of validated elements, validated elements, parameters)
             var exp = new RegExp (params);    //instantiation of regular objects, parameters for user-passed regular expressions
            return Exp.test (value);                    //Test Match
       },
        format error);

$ (function () {
$ ("#signupForm"). Validate (

The new section in the previous example
{
Rules: {
TxtPassword1: "Required",//password 1 required

                TxtPassword2: {    //Password 2 describes more than 1 items using the object type
                     required:true, //required, this can be an anonymous method
                     Equalto: "# TxtPassword1 ", //must be equal to password 1
                     rangelength: [5, 10],   //length 5-10 between
                     Regex: "^ w+$          //Use custom validation rules
                },

                txtemail: "Email",   //e-mail must be legal
                 Sex: "Required"
           },
             messages: { //corresponds to the error message above
                 txtPassword1: "You must fill in",

TxtPassword2: {
Required: "You must fill in",
Equalto: "Password Inconsistent",
Rangelength: "String length must be between {0} and {1}",
Regex: "Password can only be numbers, letters and underscores"
}
},

Errorplacement:function (Error, Element) {//Specify error message Location
if (Element.is (': Radio ') | | | element.is (': CheckBox ')) {//If radio or checkbox
var Eid = element.attr (' name '); Get the element's Name property
Error.appendto (Element.parent ()); To add an error message after the parent node of the current element
} else {
Error.insertafter (Element);
}
},

Debug:false,//if modified to true, the form will not be submitted
Submithandler:function () {
Alert ("Beginning of submission");
}
});
});
</script>

<style type= "Text/css Tutorial" >
*
{
font-size:14px;
}
#signupForm Label.error
{
color:red;
font-size:13px;
margin-left:5px;
padding-left:16px;
Background:url ("Error.png") left no-repeat;
}
</style>
<body>
<form id= "Signupform" method= "Get" action= "" >
<fieldset>
<legend> User Registration </legend>
<p>
<label for= "TxtPassword1" >
Password 1</label>
<input id= "TxtPassword1" name= "TxtPassword1" type= "text"/>
</p>
<p>
<label for= "TxtPassword2" >
Password 2</label>
<input id= "TxtPassword2" name= "TxtPassword2" type= "text"/>
</p>
<p>
<label for= "Txtemail" >
Email </label>
<input id= "Txtemail" name= "Txtemail"/>
</p>
<p>
<label for= "Sex" >
Sex </label>
<input type= "Radio" id= "Rdofemale" name= "sex"/> Male
<input type= "Radio" id= "Rdomale" name= "Sex"/> Female
</p>
<p>
<label for= "Txtage" >
Age </label>
<input id= "Txtage" name= "Txtage" min= "a" max= ""/>
<!--age is only between 18-45-->
</p>
<p>
<input type= "Submit" value= "submitted"/>
</p>
</fieldset>
</form>
</body>


HTML is the same as the previous example
Added a messages item based on the previous example, specifying a value for the object type, which corresponds to the rules basic, purposely not specifying an email error message, and the results are as follows:

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.