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