commit extjs-2.2.1
[extjs.git] / source / widgets / form / VTypes.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.form.VTypes\r
11  * This is a singleton object which contains a set of commonly used field validation functions.\r
12  * The validations provided are basic and intended to be easily customizable and extended. To add\r
13  * your own custom VType:<pre><code>\r
14 Ext.apply(Ext.form.VTypes, {\r
15     IPAddress:  function(v) {\r
16         return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);\r
17     },\r
18     IPAddressText: 'Must be a numeric IP address'\r
19 });\r
20 </code></pre>\r
21  * @singleton\r
22  */\r
23 Ext.form.VTypes = function(){\r
24     // closure these in so they are only created once.\r
25     var alpha = /^[a-zA-Z_]+$/;\r
26     var alphanum = /^[a-zA-Z0-9_]+$/;\r
27     var email = /^([\w]+)(\.[\w]+)*@([\w\-]+\.){1,5}([A-Za-z]){2,4}$/;\r
28     var url = /(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;\r
29 \r
30     // All these messages and functions are configurable\r
31     return {\r
32         /**\r
33          * The function used to validate email addresses.  Note that this is a very basic validation -- complete\r
34          * validation per the email RFC specifications is very complex and beyond the scope of this class, although\r
35          * this function can be overridden if a more comprehensive validation scheme is desired.  See the validation\r
36          * section of the <a href="http://en.wikipedia.org/wiki/E-mail_address">Wikipedia article on email addresses</a> \r
37          * for additional information.\r
38          * @param {String} value The email address\r
39          */\r
40         'email' : function(v){\r
41             return email.test(v);\r
42         },\r
43         /**\r
44          * The error text to display when the email validation function returns false\r
45          * @type String\r
46          */\r
47         'emailText' : 'This field should be an e-mail address in the format "user@domain.com"',\r
48         /**\r
49          * The keystroke filter mask to be applied on email input.  See the {@link #email} method for \r
50          * information about more complex email validation.\r
51          * @type RegExp\r
52          */\r
53         'emailMask' : /[a-z0-9_\.\-@]/i,\r
54 \r
55         /**\r
56          * The function used to validate URLs\r
57          * @param {String} value The URL\r
58          */\r
59         'url' : function(v){\r
60             return url.test(v);\r
61         },\r
62         /**\r
63          * The error text to display when the url validation function returns false\r
64          * @type String\r
65          */\r
66         'urlText' : 'This field should be a URL in the format "http:/'+'/www.domain.com"',\r
67         \r
68         /**\r
69          * The function used to validate alpha values\r
70          * @param {String} value The value\r
71          */\r
72         'alpha' : function(v){\r
73             return alpha.test(v);\r
74         },\r
75         /**\r
76          * The error text to display when the alpha validation function returns false\r
77          * @type String\r
78          */\r
79         'alphaText' : 'This field should only contain letters and _',\r
80         /**\r
81          * The keystroke filter mask to be applied on alpha input\r
82          * @type RegExp\r
83          */\r
84         'alphaMask' : /[a-z_]/i,\r
85 \r
86         /**\r
87          * The function used to validate alphanumeric values\r
88          * @param {String} value The value\r
89          */\r
90         'alphanum' : function(v){\r
91             return alphanum.test(v);\r
92         },\r
93         /**\r
94          * The error text to display when the alphanumeric validation function returns false\r
95          * @type String\r
96          */\r
97         'alphanumText' : 'This field should only contain letters, numbers and _',\r
98         /**\r
99          * The keystroke filter mask to be applied on alphanumeric input\r
100          * @type RegExp\r
101          */\r
102         'alphanumMask' : /[a-z0-9_]/i\r
103     };\r
104 }();