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>
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);
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
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);
29 IPAddressText: 'Must be a numeric IP address',
30 IPAddressMask: /[\d\.]/i
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;
42 // All these messages and functions are configurable
45 alternateClassName: 'Ext.form.VTypes',
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'
55 * @param {String} value The email address
56 * @return {Boolean} true if the RegExp test passed, and false if not.
58 'email' : function(v){
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>
66 'emailText' : 'This field should be an e-mail address in the format "user@example.com"',
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>
73 'emailMask' : /[a-z0-9_\.\-@\+]/i,
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.
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>
88 'urlText' : 'This field should be a URL in the format "http:/'+'/www.example.com"',
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.
95 'alpha' : function(v){
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>
103 'alphaText' : 'This field should only contain letters and _',
105 * The keystroke filter mask to be applied on alpha input. Defaults to:
109 'alphaMask' : /[a-z_]/i,
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.
116 'alphanum' : function(v){
117 return alphanum.test(v);
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>
124 'alphanumText' : 'This field should only contain letters, numbers and _',
126 * The keystroke filter mask to be applied on alphanumeric input. Defaults to:
127 * <tt>/[a-z0-9_]/i</tt>
130 'alphanumMask' : /[a-z0-9_]/i