]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/src/form/field/VTypes.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / src / form / field / VTypes.js
CommitLineData
6527f429
DM
1/**\r
2 * @singleton\r
3 * @alternateClassName Ext.form.VTypes\r
4 *\r
5 * This is a singleton object which contains a set of commonly used field validation functions\r
6 * and provides a mechanism for creating reusable custom field validations.\r
7 * The following field validation functions are provided out of the box:\r
8 *\r
9 * - {@link #alpha}\r
10 * - {@link #alphanum}\r
11 * - {@link #email}\r
12 * - {@link #url}\r
13 *\r
14 * VTypes can be applied to a {@link Ext.form.field.Text Text Field} using the `{@link Ext.form.field.Text#vtype vtype}` configuration:\r
15 *\r
16 * Ext.create('Ext.form.field.Text', {\r
17 * fieldLabel: 'Email Address',\r
18 * name: 'email',\r
19 * vtype: 'email' // applies email validation rules to this field\r
20 * });\r
21 *\r
22 * To create custom VTypes:\r
23 *\r
24 * // custom Vtype for vtype:'time'\r
25 * Ext.define('Override.form.field.VTypes', {\r
26 * override: 'Ext.form.field.VTypes',\r
27 *\r
28 * // vtype validation function\r
29 * time: function(value) {\r
30 * return this.timeRe.test(value);\r
31 * },\r
32 * // RegExp for the value to be tested against within the validation function\r
33 * timeRe: /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i,\r
34 * // vtype Text property: The error text to display when the validation function returns false\r
35 * timeText: 'Not a valid time. Must be in the format "12:34 PM".',\r
36 * // vtype Mask property: The keystroke filter mask\r
37 * timeMask: /[\d\s:amp]/i\r
38 * });\r
39 *\r
40 * In the above example the `time` function is the validator that will run when field validation occurs,\r
41 * `timeText` is the error message, and `timeMask` limits what characters can be typed into the field.\r
42 * Note that the `Text` and `Mask` functions must begin with the same name as the validator function.\r
43 *\r
44 * Using a custom validator is the same as using one of the build-in validators - just use the name of the validator function\r
45 * as the `{@link Ext.form.field.Text#vtype vtype}` configuration on a {@link Ext.form.field.Text Text Field}:\r
46 *\r
47 * Ext.create('Ext.form.field.Text', {\r
48 * fieldLabel: 'Departure Time',\r
49 * name: 'departureTime',\r
50 * vtype: 'time' // applies custom time validation rules to this field\r
51 * });\r
52 *\r
53 * Another example of a custom validator:\r
54 *\r
55 * // custom Vtype for vtype:'IPAddress'\r
56 * Ext.define('Override.form.field.VTypes', {\r
57 * override: 'Ext.form.field.VTypes',\r
58 *\r
59 * IPAddress: function(value) {\r
60 * return this.IPAddressRe.test(value);\r
61 * },\r
62 * IPAddressRe: /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/,\r
63 * IPAddressText: 'Must be a numeric IP address',\r
64 * IPAddressMask: /[\d\.]/i\r
65 * });\r
66 *\r
67 * It's important to note that using {@link Ext#define Ext.define()} with the {@link Ext.Class#override override} property\r
68 * means that the custom validator function as well as `Text` and `Mask` fields are added as properties\r
69 * of the `Ext.form.field.VTypes` singleton.\r
70 */\r
71Ext.define('Ext.form.field.VTypes', (function(){\r
72 // closure these in so they are only created once.\r
73 var alpha = /^[a-zA-Z_]+$/,\r
74 alphanum = /^[a-zA-Z0-9_]+$/,\r
75\r
76 // http://en.wikipedia.org/wiki/Email_address#Local_part\r
77 // http://stackoverflow.com/a/2049510\r
78 // http://isemail.info/\r
79 // http://blog.stevenlevithan.com/archives/capturing-vs-non-capturing-groups\r
80 //\r
81 // 1. Can begin with a double-quote ONLY IF the local part also ends in a double-quote.\r
82 // 2. Can NOT BEGIN with a period.\r
83 // 3. Can NOT END with a period.\r
84 // 4. Can not have MORE THAN ONE period in a row.\r
85 //\r
86 // Let's break this down:\r
87 //\r
88 // ^(")?\r
89 // The local part may begin with double-quotes, but only if it also ends with it.\r
90 // See the back-reference. Capturing.\r
91 //\r
92 // (?:[^\."])\r
93 // Here we've defined that the local part cannot begin with a period or a double-quote. Non-capturing.\r
94 //\r
95 // (?:(?:[\.])?(?:[\w\-!#$%&'*+/=?^_`{|}~]))*\r
96 // After the first character is matched, the regex ensures that there is not more than one period\r
97 // in a row. Then, this nested grouping allows for zero or more of the accepted characters.\r
98 // NOTE that this also ensures that any character not defined in the character class\r
99 // is invalid as an ending character for the local part (such as the period).\r
100 //\r
101 // \1@\r
102 // The local part of the address is a backreference to the first (and only) capturing group that allows\r
103 // for a double-quote to wrap the local part of an email address.\r
104 email = /^(")?(?:[^\."\s])(?:(?:[\.])?(?:[\w\-!#$%&'*+/=?^_`{|}~]))*\1@(\w[\-\w]*\.){1,5}([A-Za-z]){2,6}$/,\r
105 url = /(((^https?)|(^ftp)):\/\/((([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*)|(localhost|LOCALHOST))\/?)/i;\r
106\r
107 // All these messages and functions are configurable\r
108 return {\r
109 singleton: true,\r
110 alternateClassName: 'Ext.form.VTypes',\r
111\r
112 /**\r
113 * The function used to validate email addresses. Note that complete validation per the email RFC\r
114 * specifications is very complex and beyond the scope of this class, although this function can be\r
115 * overridden if a more comprehensive validation scheme is desired. See the validation section\r
116 * of the [Wikipedia article on email addresses][1] for additional information. This implementation is\r
117 * intended to validate the following types of emails:\r
118 *\r
119 * - `barney@example.de`\r
120 * - `barney.rubble@example.com`\r
121 * - `barney-rubble@example.coop`\r
122 * - `barney+rubble@example.com`\r
123 * - `barney'rubble@example.com`\r
124 * - `b.arne.y_r.ubbl.e@example.com`\r
125 * - `barney4rubble@example.com`\r
126 * - `barney4rubble!@example.com`\r
127 * - `_barney+rubble@example.com`\r
128 * - `"barney+rubble"@example.com`\r
129 *\r
130 * [1]: http://en.wikipedia.org/wiki/E-mail_address\r
131 *\r
132 * @param {String} value The email address\r
133 * @return {Boolean} true if the RegExp test passed, and false if not.\r
134 */\r
135 'email' : function(value){\r
136 return email.test(value);\r
137 },\r
138 //<locale>\r
139 /**\r
140 * @property {String} emailText\r
141 * The error text to display when the email validation function returns false.\r
142 * Defaults to: 'This field should be an e-mail address in the format "user@example.com"'\r
143 */\r
144 'emailText' : 'This field should be an e-mail address in the format "user@example.com"',\r
145 //</locale>\r
146 /**\r
147 * @property {RegExp} emailMask\r
148 * The keystroke filter mask to be applied on email input. See the {@link #email} method for information about\r
149 * more complex email validation. Defaults to: /[a-z0-9_\.\-@]/i\r
150 */\r
151 'emailMask' : /[\w.\-@'"!#$%&'*+/=?^_`{|}~]/i,\r
152\r
153 /**\r
154 * The function used to validate URLs\r
155 * @param {String} value The URL\r
156 * @return {Boolean} true if the RegExp test passed, and false if not.\r
157 */\r
158 'url' : function(value){\r
159 return url.test(value);\r
160 },\r
161 //<locale>\r
162 /**\r
163 * @property {String} urlText\r
164 * The error text to display when the url validation function returns false.\r
165 * Defaults to: 'This field should be a URL in the format "http:/'+'/www.example.com"'\r
166 */\r
167 'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',\r
168 //</locale>\r
169\r
170 /**\r
171 * The function used to validate alpha values\r
172 * @param {String} value The value\r
173 * @return {Boolean} true if the RegExp test passed, and false if not.\r
174 */\r
175 'alpha' : function(value){\r
176 return alpha.test(value);\r
177 },\r
178 //<locale>\r
179 /**\r
180 * @property {String} alphaText\r
181 * The error text to display when the alpha validation function returns false.\r
182 * Defaults to: 'This field should only contain letters and _'\r
183 */\r
184 'alphaText' : 'This field should only contain letters and _',\r
185 //</locale>\r
186 /**\r
187 * @property {RegExp} alphaMask\r
188 * The keystroke filter mask to be applied on alpha input. Defaults to: /[a-z_]/i\r
189 */\r
190 'alphaMask' : /[a-z_]/i,\r
191\r
192 /**\r
193 * The function used to validate alphanumeric values\r
194 * @param {String} value The value\r
195 * @return {Boolean} true if the RegExp test passed, and false if not.\r
196 */\r
197 'alphanum' : function(value){\r
198 return alphanum.test(value);\r
199 },\r
200 //<locale>\r
201 /**\r
202 * @property {String} alphanumText\r
203 * The error text to display when the alphanumeric validation function returns false.\r
204 * Defaults to: 'This field should only contain letters, numbers and _'\r
205 */\r
206 'alphanumText' : 'This field should only contain letters, numbers and _',\r
207 //</locale>\r
208 /**\r
209 * @property {RegExp} alphanumMask\r
210 * The keystroke filter mask to be applied on alphanumeric input. Defaults to: /[a-z0-9_]/i\r
211 */\r
212 'alphanumMask' : /[a-z0-9_]/i\r
213 };\r
214}()));\r