source: Dev/branches/rest-dojo-ui/client/dojox/validate/regexp.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 11.2 KB
Line 
1define(["dojo/_base/lang", "dojo/regexp", "dojox/main"],
2  function(lang, regexp, dojox){
3
4var dxregexp = lang.getObject("validate.regexp", true, dojox);
5dxregexp = dojox.validate.regexp = {
6       
7        ipAddress: function(/*Object?*/flags){
8                // summary: Builds a RE that matches an IP Address
9                //
10                // description:
11                //  Supports 5 formats for IPv4: dotted decimal, dotted hex, dotted octal, decimal and hexadecimal.
12                //  Supports 2 formats for Ipv6.
13                //
14                // flags  An object.  All flags are boolean with default = true.
15                //    flags.allowDottedDecimal  Example, 207.142.131.235.  No zero padding.
16                //    flags.allowDottedHex  Example, 0x18.0x11.0x9b.0x28.  Case insensitive.  Zero padding allowed.
17                //    flags.allowDottedOctal  Example, 0030.0021.0233.0050.  Zero padding allowed.
18                //    flags.allowDecimal  Example, 3482223595.  A decimal number between 0-4294967295.
19                //    flags.allowHex  Example, 0xCF8E83EB.  Hexadecimal number between 0x0-0xFFFFFFFF.
20                //      Case insensitive.  Zero padding allowed.
21                //    flags.allowIPv6   IPv6 address written as eight groups of four hexadecimal digits.
22                //      FIXME: ipv6 can be written multiple ways IIRC
23                //    flags.allowHybrid   IPv6 address written as six groups of four hexadecimal digits
24                //      followed by the usual 4 dotted decimal digit notation of IPv4. x:x:x:x:x:x:d.d.d.d
25
26                // assign default values to missing paramters
27                flags = (typeof flags == "object") ? flags : {};
28                if(typeof flags.allowDottedDecimal != "boolean"){ flags.allowDottedDecimal = true; }
29                if(typeof flags.allowDottedHex != "boolean"){ flags.allowDottedHex = true; }
30                if(typeof flags.allowDottedOctal != "boolean"){ flags.allowDottedOctal = true; }
31                if(typeof flags.allowDecimal != "boolean"){ flags.allowDecimal = true; }
32                if(typeof flags.allowHex != "boolean"){ flags.allowHex = true; }
33                if(typeof flags.allowIPv6 != "boolean"){ flags.allowIPv6 = true; }
34                if(typeof flags.allowHybrid != "boolean"){ flags.allowHybrid = true; }
35
36                // decimal-dotted IP address RE.
37                var dottedDecimalRE =
38                        // Each number is between 0-255.  Zero padding is not allowed.
39                        "((\\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])";
40
41                // dotted hex IP address RE.  Each number is between 0x0-0xff.  Zero padding is allowed, e.g. 0x00.
42                var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]";
43
44                // dotted octal IP address RE.  Each number is between 0000-0377.
45                // Zero padding is allowed, but each number must have at least 4 characters.
46                var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]";
47
48                // decimal IP address RE.  A decimal number between 0-4294967295.
49                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}|" +
50                        "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])";
51
52                // hexadecimal IP address RE.
53                // A hexadecimal number between 0x0-0xFFFFFFFF. Case insensitive.  Zero padding is allowed.
54                var hexRE = "0[xX]0*[\\da-fA-F]{1,8}";
55
56                // IPv6 address RE.
57                // The format is written as eight groups of four hexadecimal digits, x:x:x:x:x:x:x:x,
58                // where x is between 0000-ffff. Zero padding is optional. Case insensitive.
59                var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}";
60
61                // IPv6/IPv4 Hybrid address RE.
62                // The format is written as six groups of four hexadecimal digits,
63                // followed by the 4 dotted decimal IPv4 format. x:x:x:x:x:x:d.d.d.d
64                var hybridRE = "([\\da-fA-F]{1,4}\\:){6}" +
65                        "((\\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])";
66
67                // Build IP Address RE
68                var a = [];
69                if(flags.allowDottedDecimal){ a.push(dottedDecimalRE); }
70                if(flags.allowDottedHex){ a.push(dottedHexRE); }
71                if(flags.allowDottedOctal){ a.push(dottedOctalRE); }
72                if(flags.allowDecimal){ a.push(decimalRE); }
73                if(flags.allowHex){ a.push(hexRE); }
74                if(flags.allowIPv6){ a.push(ipv6RE); }
75                if(flags.allowHybrid){ a.push(hybridRE); }
76
77                var ipAddressRE = "";
78                if(a.length > 0){
79                        ipAddressRE = "(" + a.join("|") + ")";
80                }
81                return ipAddressRE; // String
82        },
83
84        host: function(/*Object?*/flags){
85                // summary: Builds a RE that matches a host
86                // description: A host is a named host (A-z0-9_- but not starting with -), a domain name or an IP address, possibly followed by a port number.
87                // flags: An object.
88                //        flags.allowNamed Allow a named host for local networks. Default is false.
89                //    flags.allowIP  Allow an IP address for hostname.  Default is true.
90                //    flags.allowLocal  Allow the host to be "localhost".  Default is false.
91                //    flags.allowPort  Allow a port number to be present.  Default is true.
92                //    flags in regexp.ipAddress can be applied.
93
94                // assign default values to missing paramters
95                flags = (typeof flags == "object") ? flags : {};
96
97                if(typeof flags.allowIP != "boolean"){ flags.allowIP = true; }
98                if(typeof flags.allowLocal != "boolean"){ flags.allowLocal = false; }
99                if(typeof flags.allowPort != "boolean"){ flags.allowPort = true; }
100                if(typeof flags.allowNamed != "boolean"){ flags.allowNamed = false; }
101
102                //TODO: support unicode hostnames?
103                // Domain name labels can not end with a dash.
104                var domainLabelRE = "(?:[\\da-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)";
105                var domainNameRE = "(?:[a-zA-Z](?:[-\\da-zA-Z]{0,6}[\\da-zA-Z])?)"; // restricted version to allow backwards compatibility with allowLocal, allowIP
106
107                // port number RE
108                var portRE = flags.allowPort ? "(\\:\\d+)?" : "";
109
110                // build host RE
111                var hostNameRE = "((?:" + domainLabelRE + "\\.)+" + domainNameRE + "\\.?)";
112                if(flags.allowIP){ hostNameRE += "|" +  dxregexp.ipAddress(flags); }
113                if(flags.allowLocal){ hostNameRE += "|localhost"; }
114                if(flags.allowNamed){ hostNameRE += "|^[^-][a-zA-Z0-9_-]*"; }
115                return "(" + hostNameRE + ")" + portRE; // String
116
117        },
118
119        url: function(/*Object?*/flags){
120                // summary: Builds a regular expression that matches a URL
121                //
122                // flags: An object
123                //    flags.scheme  Can be true, false, or [true, false].
124                //      This means: required, not allowed, or match either one.
125                //    flags in regexp.host can be applied.
126                //    flags in regexp.ipAddress can be applied.
127
128                // assign default values to missing paramters
129                flags = (typeof flags == "object") ? flags : {};
130                if(!("scheme" in flags)){ flags.scheme = [true, false]; }
131
132                // Scheme RE
133                var protocolRE = regexp.buildGroupRE(flags.scheme,
134                        function(q){ if(q){ return "(https?|ftps?)\\://"; } return ""; }
135                );
136
137                // Path and query and anchor RE
138                var pathRE = "(/(?:[^?#\\s/]+/)*(?:[^?#\\s/]+(?:\\?[^?#\\s/]*)?(?:#[A-Za-z][\\w.:-]*)?)?)?";
139
140                return protocolRE + dxregexp.host(flags) + pathRE;
141        },
142
143        emailAddress: function(/*Object?*/flags){
144
145                // summary: Builds a regular expression that matches an email address
146                //
147                //flags: An object
148                //    flags.allowCruft  Allow address like <mailto:foo@yahoo.com>.  Default is false.
149                //    flags in regexp.host can be applied.
150                //    flags in regexp.ipAddress can be applied.
151
152                // assign default values to missing paramters
153                flags = (typeof flags == "object") ? flags : {};
154                if (typeof flags.allowCruft != "boolean") { flags.allowCruft = false; }
155                flags.allowPort = false; // invalid in email addresses
156
157                // user name RE per rfc5322
158                var usernameRE = "([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+";
159
160                // build emailAddress RE
161                var emailAddressRE = usernameRE + "@" + dxregexp.host(flags);
162
163                // Allow email addresses with cruft
164                if ( flags.allowCruft ) {
165                        emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
166                }
167
168                return emailAddressRE; // String
169        },
170
171        emailAddressList: function(/*Object?*/flags){
172                // summary: Builds a regular expression that matches a list of email addresses.
173                //
174                // flags: An object.
175                //    flags.listSeparator  The character used to separate email addresses.  Default is ";", ",", "\n" or " ".
176                //    flags in regexp.emailAddress can be applied.
177                //    flags in regexp.host can be applied.
178                //    flags in regexp.ipAddress can be applied.
179
180                // assign default values to missing paramters
181                flags = (typeof flags == "object") ? flags : {};
182                if(typeof flags.listSeparator != "string"){ flags.listSeparator = "\\s;,"; }
183
184                // build a RE for an Email Address List
185                var emailAddressRE = dxregexp.emailAddress(flags);
186                var emailAddressListRE = "(" + emailAddressRE + "\\s*[" + flags.listSeparator + "]\\s*)*" +
187                        emailAddressRE + "\\s*[" + flags.listSeparator + "]?\\s*";
188
189                return emailAddressListRE; // String
190        },
191       
192        numberFormat: function(/*Object?*/flags){
193                // summary: Builds a regular expression to match any sort of number based format
194                // description:
195                //  Use this method for phone numbers, social security numbers, zip-codes, etc.
196                //  The RE can match one format or one of multiple formats.
197                //
198                //  Format
199                //    #        Stands for a digit, 0-9.
200                //    ?        Stands for an optional digit, 0-9 or nothing.
201                //    All other characters must appear literally in the expression.
202                //
203                //  Example
204                //    "(###) ###-####"       ->   (510) 542-9742
205                //    "(###) ###-#### x#???" ->   (510) 542-9742 x153
206                //    "###-##-####"          ->   506-82-1089       i.e. social security number
207                //    "#####-####"           ->   98225-1649        i.e. zip code
208                //
209                // flags:  An object
210                //    flags.format  A string or an Array of strings for multiple formats.
211
212                // assign default values to missing paramters
213                flags = (typeof flags == "object") ? flags : {};
214                if(typeof flags.format == "undefined"){ flags.format = "###-###-####"; }
215
216                // Converts a number format to RE.
217                var digitRE = function(format){
218                        // escape all special characters, except '?'
219                        return regexp.escapeString(format, "?")
220                                // Now replace '?' with Regular Expression
221                                .replace(/\?/g, "\\d?")
222                                // replace # with Regular Expression
223                                .replace(/#/g, "\\d")
224                        ;
225                };
226
227                // build RE for multiple number formats
228                return regexp.buildGroupRE(flags.format, digitRE); //String
229        },
230       
231        ca: {
232
233                postalCode: function(){
234                        // summary: String regular Express to match Canadain Postal Codes
235                        return "([A-Z][0-9][A-Z] [0-9][A-Z][0-9])";
236                },
237
238                province: function(){
239                        // summary: a regular expression to match Canadian Province Abbreviations
240                        return "(AB|BC|MB|NB|NL|NS|NT|NU|ON|PE|QC|SK|YT)";
241                }
242
243        },
244       
245        us:{
246
247                state: function(/*Object?*/flags){
248                        // summary: A regular expression to match US state and territory abbreviations
249                        //
250                        // flags  An object.
251                        //    flags.allowTerritories  Allow Guam, Puerto Rico, etc.  Default is true.
252                        //    flags.allowMilitary  Allow military 'states', e.g. Armed Forces Europe (AE).  Default is true.
253
254                        // assign default values to missing paramters
255                        flags = (typeof flags == "object") ? flags : {};
256                        if(typeof flags.allowTerritories != "boolean"){ flags.allowTerritories = true; }
257                        if(typeof flags.allowMilitary != "boolean"){ flags.allowMilitary = true; }
258
259                        // state RE
260                        var statesRE =
261                                "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|" +
262                                "NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
263
264                        // territories RE
265                        var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI";
266
267                        // military states RE
268                        var militaryRE = "AA|AE|AP";
269
270                        // Build states and territories RE
271                        if(flags.allowTerritories){ statesRE += "|" + territoriesRE; }
272                        if(flags.allowMilitary){ statesRE += "|" + militaryRE; }
273
274                        return "(" + statesRE + ")"; // String
275                }
276
277        }
278       
279};
280
281return dxregexp;
282
283});
Note: See TracBrowser for help on using the repository browser.