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