2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.validate.regexp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.validate.regexp"] = true;
|
|
|
3 |
dojo.provide("dojox.validate.regexp");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojo.regexp");
|
|
|
6 |
|
|
|
7 |
// *** Regular Expression Generator does not entirely live here ***
|
|
|
8 |
// FIXME: is this useful enough to be in /dojox/regexp/_base.js, or
|
|
|
9 |
// should it respect namespace and be dojox.validate.regexp?
|
|
|
10 |
// some say a generic regexp to match zipcodes and urls would be useful
|
|
|
11 |
// others would say it's a spare tire.
|
|
|
12 |
dojox.regexp = { ca: {}, us: {} };
|
|
|
13 |
|
|
|
14 |
dojox.regexp.tld = function(/*Object?*/flags){
|
|
|
15 |
// summary: Builds a RE that matches a top-level domain
|
|
|
16 |
//
|
|
|
17 |
// flags:
|
|
|
18 |
// flags.allowCC Include 2 letter country code domains. Default is true.
|
|
|
19 |
// flags.allowGeneric Include the generic domains. Default is true.
|
|
|
20 |
// flags.allowInfra Include infrastructure domains. Default is true.
|
|
|
21 |
|
|
|
22 |
// assign default values to missing paramters
|
|
|
23 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
24 |
if(typeof flags.allowCC != "boolean"){ flags.allowCC = true; }
|
|
|
25 |
if(typeof flags.allowInfra != "boolean"){ flags.allowInfra = true; }
|
|
|
26 |
if(typeof flags.allowGeneric != "boolean"){ flags.allowGeneric = true; }
|
|
|
27 |
|
|
|
28 |
// Infrastructure top-level domain - only one at present
|
|
|
29 |
var infraRE = "arpa";
|
|
|
30 |
|
|
|
31 |
// Generic top-level domains RE.
|
|
|
32 |
var genericRE =
|
|
|
33 |
"aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|xxx|jobs|mobi|post";
|
|
|
34 |
|
|
|
35 |
// Country Code top-level domains RE
|
|
|
36 |
var ccRE =
|
|
|
37 |
"ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|" +
|
|
|
38 |
"bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|" +
|
|
|
39 |
"ec|ee|eg|er|eu|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|"
|
|
|
40 |
+
|
|
|
41 |
"gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|" +
|
|
|
42 |
"la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|" +
|
|
|
43 |
"my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|" +
|
|
|
44 |
"re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|" +
|
|
|
45 |
"tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw";
|
|
|
46 |
|
|
|
47 |
// Build top-level domain RE
|
|
|
48 |
var a = [];
|
|
|
49 |
if(flags.allowInfra){ a.push(infraRE); }
|
|
|
50 |
if(flags.allowGeneric){ a.push(genericRE); }
|
|
|
51 |
if(flags.allowCC){ a.push(ccRE); }
|
|
|
52 |
|
|
|
53 |
var tldRE = "";
|
|
|
54 |
if (a.length > 0) {
|
|
|
55 |
tldRE = "(" + a.join("|") + ")";
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
return tldRE; // String
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
dojox.regexp.ipAddress = function(/*Object?*/flags){
|
|
|
62 |
// summary: Builds a RE that matches an IP Address
|
|
|
63 |
//
|
|
|
64 |
// description:
|
|
|
65 |
// Supports 5 formats for IPv4: dotted decimal, dotted hex, dotted octal, decimal and hexadecimal.
|
|
|
66 |
// Supports 2 formats for Ipv6.
|
|
|
67 |
//
|
|
|
68 |
// flags An object. All flags are boolean with default = true.
|
|
|
69 |
// flags.allowDottedDecimal Example, 207.142.131.235. No zero padding.
|
|
|
70 |
// flags.allowDottedHex Example, 0x18.0x11.0x9b.0x28. Case insensitive. Zero padding allowed.
|
|
|
71 |
// flags.allowDottedOctal Example, 0030.0021.0233.0050. Zero padding allowed.
|
|
|
72 |
// flags.allowDecimal Example, 3482223595. A decimal number between 0-4294967295.
|
|
|
73 |
// flags.allowHex Example, 0xCF8E83EB. Hexadecimal number between 0x0-0xFFFFFFFF.
|
|
|
74 |
// Case insensitive. Zero padding allowed.
|
|
|
75 |
// flags.allowIPv6 IPv6 address written as eight groups of four hexadecimal digits.
|
|
|
76 |
// FIXME: ipv6 can be written multiple ways IIRC
|
|
|
77 |
// flags.allowHybrid IPv6 address written as six groups of four hexadecimal digits
|
|
|
78 |
// followed by the usual 4 dotted decimal digit notation of IPv4. x:x:x:x:x:x:d.d.d.d
|
|
|
79 |
|
|
|
80 |
// assign default values to missing paramters
|
|
|
81 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
82 |
if(typeof flags.allowDottedDecimal != "boolean"){ flags.allowDottedDecimal = true; }
|
|
|
83 |
if(typeof flags.allowDottedHex != "boolean"){ flags.allowDottedHex = true; }
|
|
|
84 |
if(typeof flags.allowDottedOctal != "boolean"){ flags.allowDottedOctal = true; }
|
|
|
85 |
if(typeof flags.allowDecimal != "boolean"){ flags.allowDecimal = true; }
|
|
|
86 |
if(typeof flags.allowHex != "boolean"){ flags.allowHex = true; }
|
|
|
87 |
if(typeof flags.allowIPv6 != "boolean"){ flags.allowIPv6 = true; }
|
|
|
88 |
if(typeof flags.allowHybrid != "boolean"){ flags.allowHybrid = true; }
|
|
|
89 |
|
|
|
90 |
// decimal-dotted IP address RE.
|
|
|
91 |
var dottedDecimalRE =
|
|
|
92 |
// Each number is between 0-255. Zero padding is not allowed.
|
|
|
93 |
"((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
|
|
|
94 |
|
|
|
95 |
// dotted hex IP address RE. Each number is between 0x0-0xff. Zero padding is allowed, e.g. 0x00.
|
|
|
96 |
var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]";
|
|
|
97 |
|
|
|
98 |
// dotted octal IP address RE. Each number is between 0000-0377.
|
|
|
99 |
// Zero padding is allowed, but each number must have at least 4 characters.
|
|
|
100 |
var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]";
|
|
|
101 |
|
|
|
102 |
// decimal IP address RE. A decimal number between 0-4294967295.
|
|
|
103 |
var decimalRE = "(0|[1-9]\\d{0,8}|[1-3]\\d{9}|4[01]\\d{8}|42[0-8]\\d{7}|429[0-3]\\d{6}|" +
|
|
|
104 |
"4294[0-8]\\d{5}|42949[0-5]\\d{4}|429496[0-6]\\d{3}|4294967[01]\\d{2}|42949672[0-8]\\d|429496729[0-5])";
|
|
|
105 |
|
|
|
106 |
// hexadecimal IP address RE.
|
|
|
107 |
// A hexadecimal number between 0x0-0xFFFFFFFF. Case insensitive. Zero padding is allowed.
|
|
|
108 |
var hexRE = "0[xX]0*[\\da-fA-F]{1,8}";
|
|
|
109 |
|
|
|
110 |
// IPv6 address RE.
|
|
|
111 |
// The format is written as eight groups of four hexadecimal digits, x:x:x:x:x:x:x:x,
|
|
|
112 |
// where x is between 0000-ffff. Zero padding is optional. Case insensitive.
|
|
|
113 |
var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}";
|
|
|
114 |
|
|
|
115 |
// IPv6/IPv4 Hybrid address RE.
|
|
|
116 |
// The format is written as six groups of four hexadecimal digits,
|
|
|
117 |
// followed by the 4 dotted decimal IPv4 format. x:x:x:x:x:x:d.d.d.d
|
|
|
118 |
var hybridRE = "([\\da-fA-F]{1,4}\\:){6}" +
|
|
|
119 |
"((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
|
|
|
120 |
|
|
|
121 |
// Build IP Address RE
|
|
|
122 |
var a = [];
|
|
|
123 |
if(flags.allowDottedDecimal){ a.push(dottedDecimalRE); }
|
|
|
124 |
if(flags.allowDottedHex){ a.push(dottedHexRE); }
|
|
|
125 |
if(flags.allowDottedOctal){ a.push(dottedOctalRE); }
|
|
|
126 |
if(flags.allowDecimal){ a.push(decimalRE); }
|
|
|
127 |
if(flags.allowHex){ a.push(hexRE); }
|
|
|
128 |
if(flags.allowIPv6){ a.push(ipv6RE); }
|
|
|
129 |
if(flags.allowHybrid){ a.push(hybridRE); }
|
|
|
130 |
|
|
|
131 |
var ipAddressRE = "";
|
|
|
132 |
if(a.length > 0){
|
|
|
133 |
ipAddressRE = "(" + a.join("|") + ")";
|
|
|
134 |
}
|
|
|
135 |
return ipAddressRE; // String
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
dojox.regexp.host = function(/*Object?*/flags){
|
|
|
139 |
// summary: Builds a RE that matches a host
|
|
|
140 |
// description: A host is a domain name or an IP address, possibly followed by a port number.
|
|
|
141 |
// flags: An object.
|
|
|
142 |
// flags.allowIP Allow an IP address for hostname. Default is true.
|
|
|
143 |
// flags.allowLocal Allow the host to be "localhost". Default is false.
|
|
|
144 |
// flags.allowPort Allow a port number to be present. Default is true.
|
|
|
145 |
// flags in regexp.ipAddress can be applied.
|
|
|
146 |
// flags in regexp.tld can be applied.
|
|
|
147 |
|
|
|
148 |
// assign default values to missing paramters
|
|
|
149 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
150 |
if(typeof flags.allowIP != "boolean"){ flags.allowIP = true; }
|
|
|
151 |
if(typeof flags.allowLocal != "boolean"){ flags.allowLocal = false; }
|
|
|
152 |
if(typeof flags.allowPort != "boolean"){ flags.allowPort = true; }
|
|
|
153 |
|
|
|
154 |
// Domain names can not end with a dash.
|
|
|
155 |
var domainNameRE = "([0-9a-zA-Z]([-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?\\.)+" + dojox.regexp.tld(flags);
|
|
|
156 |
|
|
|
157 |
// port number RE
|
|
|
158 |
var portRE = ( flags.allowPort ) ? "(\\:" + dojox.regexp.integer({signed: false}) + ")?" : "";
|
|
|
159 |
|
|
|
160 |
// build host RE
|
|
|
161 |
var hostNameRE = domainNameRE;
|
|
|
162 |
if(flags.allowIP){ hostNameRE += "|" + dojox.regexp.ipAddress(flags); }
|
|
|
163 |
if(flags.allowLocal){ hostNameRE += "|localhost"; }
|
|
|
164 |
|
|
|
165 |
return "(" + hostNameRE + ")" + portRE; // String
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
dojox.regexp.url = function(/*Object?*/flags){
|
|
|
169 |
// summary: Builds a regular expression that matches a URL
|
|
|
170 |
//
|
|
|
171 |
// flags: An object
|
|
|
172 |
// flags.scheme Can be true, false, or [true, false].
|
|
|
173 |
// This means: required, not allowed, or match either one.
|
|
|
174 |
// flags in regexp.host can be applied.
|
|
|
175 |
// flags in regexp.ipAddress can be applied.
|
|
|
176 |
// flags in regexp.tld can be applied.
|
|
|
177 |
|
|
|
178 |
// assign default values to missing paramters
|
|
|
179 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
180 |
if(typeof flags.scheme == "undefined"){ flags.scheme = [true, false]; }
|
|
|
181 |
|
|
|
182 |
// Scheme RE
|
|
|
183 |
var protocolRE = dojo.regexp.buildGroupRE(flags.scheme,
|
|
|
184 |
function(q){ if(q){ return "(https?|ftps?)\\://"; } return ""; }
|
|
|
185 |
);
|
|
|
186 |
|
|
|
187 |
// Path and query and anchor RE
|
|
|
188 |
var pathRE = "(/([^?#\\s/]+/)*)?([^?#\\s/]+(\\?[^?#\\s/]*)?(#[A-Za-z][\\w.:-]*)?)?";
|
|
|
189 |
|
|
|
190 |
return protocolRE + dojox.regexp.host(flags) + pathRE;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
dojox.regexp.emailAddress = function(/*Object?*/flags){
|
|
|
194 |
|
|
|
195 |
// summary: Builds a regular expression that matches an email address
|
|
|
196 |
//
|
|
|
197 |
//flags: An object
|
|
|
198 |
// flags.allowCruft Allow address like <mailto:foo@yahoo.com>. Default is false.
|
|
|
199 |
// flags in regexp.host can be applied.
|
|
|
200 |
// flags in regexp.ipAddress can be applied.
|
|
|
201 |
// flags in regexp.tld can be applied.
|
|
|
202 |
|
|
|
203 |
// assign default values to missing paramters
|
|
|
204 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
205 |
if (typeof flags.allowCruft != "boolean") { flags.allowCruft = false; }
|
|
|
206 |
flags.allowPort = false; // invalid in email addresses
|
|
|
207 |
|
|
|
208 |
// user name RE - apostrophes are valid if there's not 2 in a row
|
|
|
209 |
var usernameRE = "([\\da-zA-Z]+[-._+&'])*[\\da-zA-Z]+";
|
|
|
210 |
|
|
|
211 |
// build emailAddress RE
|
|
|
212 |
var emailAddressRE = usernameRE + "@" + dojox.regexp.host(flags);
|
|
|
213 |
|
|
|
214 |
// Allow email addresses with cruft
|
|
|
215 |
if ( flags.allowCruft ) {
|
|
|
216 |
emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return emailAddressRE; // String
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
dojox.regexp.emailAddressList = function(/*Object?*/flags){
|
|
|
223 |
// summary: Builds a regular expression that matches a list of email addresses.
|
|
|
224 |
//
|
|
|
225 |
// flags: An object.
|
|
|
226 |
// flags.listSeparator The character used to separate email addresses. Default is ";", ",", "\n" or " ".
|
|
|
227 |
// flags in regexp.emailAddress can be applied.
|
|
|
228 |
// flags in regexp.host can be applied.
|
|
|
229 |
// flags in regexp.ipAddress can be applied.
|
|
|
230 |
// flags in regexp.tld can be applied.
|
|
|
231 |
|
|
|
232 |
// assign default values to missing paramters
|
|
|
233 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
234 |
if(typeof flags.listSeparator != "string"){ flags.listSeparator = "\\s;,"; }
|
|
|
235 |
|
|
|
236 |
// build a RE for an Email Address List
|
|
|
237 |
var emailAddressRE = dojox.regexp.emailAddress(flags);
|
|
|
238 |
var emailAddressListRE = "(" + emailAddressRE + "\\s*[" + flags.listSeparator + "]\\s*)*" +
|
|
|
239 |
emailAddressRE + "\\s*[" + flags.listSeparator + "]?\\s*";
|
|
|
240 |
|
|
|
241 |
return emailAddressListRE; // String
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
dojox.regexp.us.state = function(/*Object?*/flags){
|
|
|
245 |
// summary: A regular expression to match US state and territory abbreviations
|
|
|
246 |
//
|
|
|
247 |
// flags An object.
|
|
|
248 |
// flags.allowTerritories Allow Guam, Puerto Rico, etc. Default is true.
|
|
|
249 |
// flags.allowMilitary Allow military 'states', e.g. Armed Forces Europe (AE). Default is true.
|
|
|
250 |
|
|
|
251 |
// assign default values to missing paramters
|
|
|
252 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
253 |
if(typeof flags.allowTerritories != "boolean"){ flags.allowTerritories = true; }
|
|
|
254 |
if(typeof flags.allowMilitary != "boolean"){ flags.allowMilitary = true; }
|
|
|
255 |
|
|
|
256 |
// state RE
|
|
|
257 |
var statesRE =
|
|
|
258 |
"AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|" +
|
|
|
259 |
"NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
|
|
|
260 |
|
|
|
261 |
// territories RE
|
|
|
262 |
var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI";
|
|
|
263 |
|
|
|
264 |
// military states RE
|
|
|
265 |
var militaryRE = "AA|AE|AP";
|
|
|
266 |
|
|
|
267 |
// Build states and territories RE
|
|
|
268 |
if(flags.allowTerritories){ statesRE += "|" + territoriesRE; }
|
|
|
269 |
if(flags.allowMilitary){ statesRE += "|" + militaryRE; }
|
|
|
270 |
|
|
|
271 |
return "(" + statesRE + ")"; // String
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
dojox.regexp.ca.postalCode = function(){
|
|
|
275 |
var postalRE =
|
|
|
276 |
"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]";
|
|
|
277 |
return "(" + postalRE + ")";
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
dojox.regexp.ca.province = function(){
|
|
|
281 |
// summary: a regular expression to match Canadian Province Abbreviations
|
|
|
282 |
var stateRE =
|
|
|
283 |
"AB|BC|MB|NB|NL|NS|NT|NU|ON|PE|QC|SK|YT";
|
|
|
284 |
return "(" + statesRE + ")";
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
dojox.regexp.numberFormat = function(/*Object?*/flags){
|
|
|
288 |
// summary: Builds a regular expression to match any sort of number based format
|
|
|
289 |
// description:
|
|
|
290 |
// Use this method for phone numbers, social security numbers, zip-codes, etc.
|
|
|
291 |
// The RE can match one format or one of multiple formats.
|
|
|
292 |
//
|
|
|
293 |
// Format
|
|
|
294 |
// # Stands for a digit, 0-9.
|
|
|
295 |
// ? Stands for an optional digit, 0-9 or nothing.
|
|
|
296 |
// All other characters must appear literally in the expression.
|
|
|
297 |
//
|
|
|
298 |
// Example
|
|
|
299 |
// "(###) ###-####" -> (510) 542-9742
|
|
|
300 |
// "(###) ###-#### x#???" -> (510) 542-9742 x153
|
|
|
301 |
// "###-##-####" -> 506-82-1089 i.e. social security number
|
|
|
302 |
// "#####-####" -> 98225-1649 i.e. zip code
|
|
|
303 |
//
|
|
|
304 |
// flags: An object
|
|
|
305 |
// flags.format A string or an Array of strings for multiple formats.
|
|
|
306 |
|
|
|
307 |
// assign default values to missing paramters
|
|
|
308 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
309 |
if(typeof flags.format == "undefined"){ flags.format = "###-###-####"; }
|
|
|
310 |
|
|
|
311 |
// Converts a number format to RE.
|
|
|
312 |
var digitRE = function(format){
|
|
|
313 |
// escape all special characters, except '?'
|
|
|
314 |
format = dojo.regexp.escapeString(format, "?");
|
|
|
315 |
|
|
|
316 |
// Now replace '?' with Regular Expression
|
|
|
317 |
format = format.replace(/\?/g, "\\d?");
|
|
|
318 |
|
|
|
319 |
// replace # with Regular Expression
|
|
|
320 |
format = format.replace(/#/g, "\\d");
|
|
|
321 |
|
|
|
322 |
return format; // String
|
|
|
323 |
};
|
|
|
324 |
|
|
|
325 |
// build RE for multiple number formats
|
|
|
326 |
return dojo.regexp.buildGroupRE(flags.format, digitRE); //String
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
}
|