source: Dev/trunk/src/client/dojox/validate/regexp.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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