Upgrade to ExtJS 4.0.7 - Released 10/19/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  * @singleton
17  * @alternateClassName Ext.form.VTypes
18  *
19  * This is a singleton object which contains a set of commonly used field validation functions
20  * and provides a mechanism for creating reusable custom field validations.
21  * The following field validation functions are provided out of the box:
22  *
23  * - {@link #alpha}
24  * - {@link #alphanum}
25  * - {@link #email}
26  * - {@link #url}
27  *
28  * VTypes can be applied to a {@link Ext.form.field.Text Text Field} using the `{@link Ext.form.field.Text#vtype vtype}` configuration:
29  *
30  *     Ext.create('Ext.form.field.Text', {
31  *         fieldLabel: 'Email Address',
32  *         name: 'email',
33  *         vtype: 'email' // applies email validation rules to this field
34  *     });
35  *
36  * To create custom VTypes:
37  *
38  *     // custom Vtype for vtype:'time'
39  *     var timeTest = /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i;
40  *     Ext.apply(Ext.form.field.VTypes, {
41  *         //  vtype validation function
42  *         time: function(val, field) {
43  *             return timeTest.test(val);
44  *         },
45  *         // vtype Text property: The error text to display when the validation function returns false
46  *         timeText: 'Not a valid time.  Must be in the format "12:34 PM".',
47  *         // vtype Mask property: The keystroke filter mask
48  *         timeMask: /[\d\s:amp]/i
49  *     });
50  *
51  * In the above example the `time` function is the validator that will run when field validation occurs,
52  * `timeText` is the error message, and `timeMask` limits what characters can be typed into the field.
53  * Note that the `Text` and `Mask` functions must begin with the same name as the validator function.
54  *
55  * Using a custom validator is the same as using one of the build-in validators - just use the name of the validator function
56  * as the `{@link Ext.form.field.Text#vtype vtype}` configuration on a {@link Ext.form.field.Text Text Field}:
57  *
58  *     Ext.create('Ext.form.field.Text', {
59  *         fieldLabel: 'Departure Time',
60  *         name: 'departureTime',
61  *         vtype: 'time' // applies custom time validation rules to this field
62  *     });
63  *
64  * Another example of a custom validator:
65  *
66  *     // custom Vtype for vtype:'IPAddress'
67  *     Ext.apply(Ext.form.field.VTypes, {
68  *         IPAddress:  function(v) {
69  *             return /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(v);
70  *         },
71  *         IPAddressText: 'Must be a numeric IP address',
72  *         IPAddressMask: /[\d\.]/i
73  *     });
74  *
75  * It's important to note that using {@link Ext#apply Ext.apply()} means that the custom validator function
76  * as well as `Text` and `Mask` fields are added as properties of the `Ext.form.field.VTypes` singleton.
77  */
78 Ext.define('Ext.form.field.VTypes', (function(){
79     // closure these in so they are only created once.
80     var alpha = /^[a-zA-Z_]+$/,
81         alphanum = /^[a-zA-Z0-9_]+$/,
82         email = /^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,
83         url = /(((^https?)|(^ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
84
85     // All these messages and functions are configurable
86     return {
87         singleton: true,
88         alternateClassName: 'Ext.form.VTypes',
89
90         /**
91          * The function used to validate email addresses. Note that this is a very basic validation - complete
92          * validation per the email RFC specifications is very complex and beyond the scope of this class, although this
93          * function can be overridden if a more comprehensive validation scheme is desired. See the validation section
94          * of the [Wikipedia article on email addresses][1] for additional information. This implementation is intended
95          * to validate the following emails:
96          *
97          * - `barney@example.de`
98          * - `barney.rubble@example.com`
99          * - `barney-rubble@example.coop`
100          * - `barney+rubble@example.com`
101          *
102          * [1]: http://en.wikipedia.org/wiki/E-mail_address
103          *
104          * @param {String} value The email address
105          * @return {Boolean} true if the RegExp test passed, and false if not.
106          */
107         'email' : function(v){
108             return email.test(v);
109         },
110         /**
111          * @property {String} emailText
112          * The error text to display when the email validation function returns false.
113          * Defaults to: 'This field should be an e-mail address in the format "user@example.com"'
114          */
115         'emailText' : 'This field should be an e-mail address in the format "user@example.com"',
116         /**
117          * @property {RegExp} emailMask
118          * The keystroke filter mask to be applied on email input. See the {@link #email} method for information about
119          * more complex email validation. Defaults to: /[a-z0-9_\.\-@]/i
120          */
121         'emailMask' : /[a-z0-9_\.\-@\+]/i,
122
123         /**
124          * The function used to validate URLs
125          * @param {String} value The URL
126          * @return {Boolean} true if the RegExp test passed, and false if not.
127          */
128         'url' : function(v){
129             return url.test(v);
130         },
131         /**
132          * @property {String} urlText
133          * The error text to display when the url validation function returns false.
134          * Defaults to: 'This field should be a URL in the format "http:/'+'/www.example.com"'
135          */
136         'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',
137
138         /**
139          * The function used to validate alpha values
140          * @param {String} value The value
141          * @return {Boolean} true if the RegExp test passed, and false if not.
142          */
143         'alpha' : function(v){
144             return alpha.test(v);
145         },
146         /**
147          * @property {String} alphaText
148          * The error text to display when the alpha validation function returns false.
149          * Defaults to: 'This field should only contain letters and _'
150          */
151         'alphaText' : 'This field should only contain letters and _',
152         /**
153          * @property {RegExp} alphaMask
154          * The keystroke filter mask to be applied on alpha input. Defaults to: /[a-z_]/i
155          */
156         'alphaMask' : /[a-z_]/i,
157
158         /**
159          * The function used to validate alphanumeric values
160          * @param {String} value The value
161          * @return {Boolean} true if the RegExp test passed, and false if not.
162          */
163         'alphanum' : function(v){
164             return alphanum.test(v);
165         },
166         /**
167          * @property {String} alphanumText
168          * The error text to display when the alphanumeric validation function returns false.
169          * Defaults to: 'This field should only contain letters, numbers and _'
170          */
171         'alphanumText' : 'This field should only contain letters, numbers and _',
172         /**
173          * @property {RegExp} alphanumMask
174          * The keystroke filter mask to be applied on alphanumeric input. Defaults to: /[a-z0-9_]/i
175          */
176         'alphanumMask' : /[a-z0-9_]/i
177     };
178 })());
179