开发者

How to make placeholder varablies in jquery validate 1.7?

开发者 https://www.devze.com 2022-12-30 06:24 出处:网络
Hi I am using jquery 1.4.2 and jquery validate 1.7(http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

Hi I am using jquery 1.4.2 and jquery validate 1.7(http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

Say I have this example that I just grabbed off some random site(http://www.webreference.com/programming/javascript/jquery/form_validation/)

8     <script type="text/javascript"> 
9       $(document).ready(function() { 
10        $("#form1").validate({ 
11          rules: { 
12            name: "required",// simple rule, converted to {required:true} 
13            email: {// compound rule 
14            required: true, 
15            email: true 
16          }, 
17          url: { 
18            url: true 
19          }, 
20          comment: { 
21            required: true 
22          } 
23          }, 
24          messages: { 
25            comment: "Please enter a comment." 
26          } 
27        }); 
28      }); 
29    </script> 

now is it possible to do something like this

10        $("#form1").validate({ 
           var NameHolder = "name"
11          rules: { 
12            NameHolder: "required",// simple rule, converted to {required:true} 
13            email: {// compound rule 
14            required: true, 
15            email: true 

So basically I want to make sort of a global variable to hold theses rule names( what correspond 开发者_JS百科to the names on that html control).

My concern is the names of html controls can change and it kinda sucks that I will have to go around and change it in many places of my code to make it work again.

So basically I am wondering is there away to make a global variable to store this name. So if I need to change the name I only have to change it in one spot in my javascript file sort of the way stopping magic numbers ?


From the documentation for "rules":

Key/value pairs defining custom rules. Key is the name of an element (or a group of checkboxes/radio buttons), value is an object consisting of rule/parameter pairs or a plain String.

In your example name: refers to an input element that has the attribute name="name". Therefore, if you consistently name form attributes then using the validate plugin in multiple areas is mostly copy and paste.

Note that each validation is unique to the form. So if you have a form on page A that has "yourName" and "yourEmail" fields, and a form on page B that has an additional "yourPhone" field, then you would want to write validation rules for each form individually. But you could like use use most of the code from form A for form B.

0

精彩评论

暂无评论...
验证码 换一张
取 消