Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / form / field / VTypes.js
1 /**
2  * @class Ext.form.field.VTypes
3  * <p>This is a singleton object which contains a set of commonly used field validation functions.
4  * The validations provided are basic and intended to be easily customizable and extended.</p>
5  * <p>To add custom VTypes specify the <code>{@link Ext.form.field.Text#vtype vtype}</code> validation
6  * test function, and optionally specify any corresponding error text to display and any keystroke
7  * filtering mask to apply. For example:</p>
8  * <pre><code>
9 // custom Vtype for vtype:'time'
10 var timeTest = /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i;
11 Ext.apply(Ext.form.field.VTypes, {
12     //  vtype validation function
13     time: function(val, field) {
14         return timeTest.test(val);
15     },
16     // vtype Text property: The error text to display when the validation function returns false
17     timeText: 'Not a valid time.  Must be in the format "12:34 PM".',
18     // vtype Mask property: The keystroke filter mask
19     timeMask: /[\d\s:amp]/i
20 });
21  * </code></pre>
22  * Another example:
23  * <pre><code>
24 // custom Vtype for vtype:'IPAddress'
25 Ext.apply(Ext.form.field.VTypes, {
26     IPAddress:  function(v) {
27         return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);
28     },
29     IPAddressText: 'Must be a numeric IP address',
30     IPAddressMask: /[\d\.]/i
31 });
32  * </code></pre>
33  * @singleton
34  */
35 Ext.define('Ext.form.field.VTypes', (function(){
36     // closure these in so they are only created once.
37     var alpha = /^[a-zA-Z_]+$/,
38         alphanum = /^[a-zA-Z0-9_]+$/,
39         email = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,
40         url = /(((^https?)|(^ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
41
42     // All these messages and functions are configurable
43     return {
44         singleton: true,
45         alternateClassName: 'Ext.form.VTypes',
46
47         /**
48          * The function used to validate email addresses.  Note that this is a very basic validation -- complete
49          * validation per the email RFC specifications is very complex and beyond the scope of this class, although
50          * this function can be overridden if a more comprehensive validation scheme is desired.  See the validation
51          * section of the <a href="http://en.wikipedia.org/wiki/E-mail_address">Wikipedia article on email addresses</a>
52          * for additional information.  This implementation is intended to validate the following emails:<tt>
53          * 'barney@example.de', 'barney.rubble@example.com', 'barney-rubble@example.coop', 'barney+rubble@example.com'
54          * </tt>.
55          * @param {String} value The email address
56          * @return {Boolean} true if the RegExp test passed, and false if not.
57          */
58         'email' : function(v){
59             return email.test(v);
60         },
61         /**
62          * The error text to display when the email validation function returns false.  Defaults to:
63          * <tt>'This field should be an e-mail address in the format "user@example.com"'</tt>
64          * @type String
65          */
66         'emailText' : 'This field should be an e-mail address in the format "user@example.com"',
67         /**
68          * The keystroke filter mask to be applied on email input.  See the {@link #email} method for
69          * information about more complex email validation. Defaults to:
70          * <tt>/[a-z0-9_\.\-@]/i</tt>
71          * @type RegExp
72          */
73         'emailMask' : /[a-z0-9_\.\-@\+]/i,
74
75         /**
76          * The function used to validate URLs
77          * @param {String} value The URL
78          * @return {Boolean} true if the RegExp test passed, and false if not.
79          */
80         'url' : function(v){
81             return url.test(v);
82         },
83         /**
84          * The error text to display when the url validation function returns false.  Defaults to:
85          * <tt>'This field should be a URL in the format "http:/'+'/www.example.com"'</tt>
86          * @type String
87          */
88         'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',
89
90         /**
91          * The function used to validate alpha values
92          * @param {String} value The value
93          * @return {Boolean} true if the RegExp test passed, and false if not.
94          */
95         'alpha' : function(v){
96             return alpha.test(v);
97         },
98         /**
99          * The error text to display when the alpha validation function returns false.  Defaults to:
100          * <tt>'This field should only contain letters and _'</tt>
101          * @type String
102          */
103         'alphaText' : 'This field should only contain letters and _',
104         /**
105          * The keystroke filter mask to be applied on alpha input.  Defaults to:
106          * <tt>/[a-z_]/i</tt>
107          * @type RegExp
108          */
109         'alphaMask' : /[a-z_]/i,
110
111         /**
112          * The function used to validate alphanumeric values
113          * @param {String} value The value
114          * @return {Boolean} true if the RegExp test passed, and false if not.
115          */
116         'alphanum' : function(v){
117             return alphanum.test(v);
118         },
119         /**
120          * The error text to display when the alphanumeric validation function returns false.  Defaults to:
121          * <tt>'This field should only contain letters, numbers and _'</tt>
122          * @type String
123          */
124         'alphanumText' : 'This field should only contain letters, numbers and _',
125         /**
126          * The keystroke filter mask to be applied on alphanumeric input.  Defaults to:
127          * <tt>/[a-z0-9_]/i</tt>
128          * @type RegExp
129          */
130         'alphanumMask' : /[a-z0-9_]/i
131     };
132 })());