| 1 |
define(["dojo/_base/lang", "dojo/regexp", "dojox/main"], |
| 2 |
function(lang, regexp, dojox){ |
| 3 |
|
| 4 |
var dxregexp = lang.getObject("validate.regexp", true, dojox); |
| 5 |
dxregexp = dojox.validate.regexp = { |
| 6 |
|
| 7 |
ipAddress: function(flags){ |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 39 |
var dottedDecimalRE = |
| 40 |
|
| 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 |
|
| 44 |
var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]"; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]"; |
| 49 |
|
| 50 |
|
| 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 |
|
| 55 |
|
| 56 |
var hexRE = "0[xX]0*[\\da-fA-F]{1,8}"; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 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; |
| 84 |
}, |
| 85 |
|
| 86 |
host: function(flags){ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 107 |
|
| 108 |
var domainLabelRE = "(?:[\\da-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)"; |
| 109 |
var domainNameRE = "(?:[a-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)"; |
| 110 |
|
| 111 |
|
| 112 |
var portRE = flags.allowPort ? "(\\:\\d+)?" : ""; |
| 113 |
|
| 114 |
|
| 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; |
| 120 |
|
| 121 |
}, |
| 122 |
|
| 123 |
url: function(flags){ |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
flags = (typeof flags == "object") ? flags : {}; |
| 134 |
if(!("scheme" in flags)){ flags.scheme = [true, false]; } |
| 135 |
|
| 136 |
|
| 137 |
var protocolRE = regexp.buildGroupRE(flags.scheme, |
| 138 |
function(q){ if(q){ return "(https?|ftps?)\\://"; } return ""; } |
| 139 |
); |
| 140 |
|
| 141 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
flags = (typeof flags == "object") ? flags : {}; |
| 157 |
if (typeof flags.allowCruft != "boolean") { flags.allowCruft = false; } |
| 158 |
flags.allowPort = false; |
| 159 |
|
| 160 |
|
| 161 |
var usernameRE = "([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+"; |
| 162 |
|
| 163 |
|
| 164 |
var emailAddressRE = usernameRE + "@" + dxregexp.host(flags); |
| 165 |
|
| 166 |
|
| 167 |
if ( flags.allowCruft ) { |
| 168 |
emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?"; |
| 169 |
} |
| 170 |
|
| 171 |
return emailAddressRE; |
| 172 |
}, |
| 173 |
|
| 174 |
emailAddressList: function(flags){ |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
flags = (typeof flags == "object") ? flags : {}; |
| 185 |
if(typeof flags.listSeparator != "string"){ flags.listSeparator = "\\s;,"; } |
| 186 |
|
| 187 |
|
| 188 |
var emailAddressRE = dxregexp.emailAddress(flags); |
| 189 |
var emailAddressListRE = "(" + emailAddressRE + "\\s*[" + flags.listSeparator + "]\\s*)*" + |
| 190 |
emailAddressRE + "\\s*[" + flags.listSeparator + "]?\\s*"; |
| 191 |
|
| 192 |
return emailAddressListRE; |
| 193 |
}, |
| 194 |
|
| 195 |
numberFormat: function(flags){ |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
flags = (typeof flags == "object") ? flags : {}; |
| 219 |
if(typeof flags.format == "undefined"){ flags.format = "###-###-####"; } |
| 220 |
|
| 221 |
|
| 222 |
var digitRE = function(format){ |
| 223 |
|
| 224 |
return regexp.escapeString(format, "?") |
| 225 |
|
| 226 |
.replace(/\?/g, "\\d?") |
| 227 |
|
| 228 |
.replace(/#/g, "\\d") |
| 229 |
; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
return regexp.buildGroupRE(flags.format, digitRE); |
| 234 |
}, |
| 235 |
|
| 236 |
ca: { |
| 237 |
|
| 238 |
postalCode: function(){ |
| 239 |
|
| 240 |
|
| 241 |
return "([A-Z][0-9][A-Z] [0-9][A-Z][0-9])"; |
| 242 |
}, |
| 243 |
|
| 244 |
province: function(){ |
| 245 |
|
| 246 |
|
| 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 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 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 |
|
| 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 |
|
| 271 |
var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI"; |
| 272 |
|
| 273 |
|
| 274 |
var militaryRE = "AA|AE|AP"; |
| 275 |
|
| 276 |
|
| 277 |
if(flags.allowTerritories){ statesRE += "|" + territoriesRE; } |
| 278 |
if(flags.allowMilitary){ statesRE += "|" + militaryRE; } |
| 279 |
|
| 280 |
return "(" + statesRE + ")"; |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
}; |
| 286 |
|
| 287 |
return dxregexp; |
| 288 |
|
| 289 |
}); |