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