95cf0a09937e6fca0316fe9e1af31bcd0dbb9673.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. if (!dojo._hasResource["dojox.validate.regexp"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.validate.regexp"] = true;
  6. dojo.provide("dojox.validate.regexp");
  7. dojo.require("dojo.regexp");
  8. // *** Regular Expression Generator does not entirely live here ***
  9. // FIXME: is this useful enough to be in /dojox/regexp/_base.js, or
  10. // should it respect namespace and be dojox.validate.regexp?
  11. // some say a generic regexp to match zipcodes and urls would be useful
  12. // others would say it's a spare tire.
  13. dojox.regexp = {
  14. ca : {},
  15. us : {}
  16. };
  17. dojox.regexp.tld = function(/* Object? */flags) {
  18. // summary: Builds a RE that matches a top-level domain
  19. //
  20. // flags:
  21. // flags.allowCC Include 2 letter country code domains. Default is true.
  22. // flags.allowGeneric Include the generic domains. Default is true.
  23. // flags.allowInfra Include infrastructure domains. Default is true.
  24. // assign default values to missing paramters
  25. flags = (typeof flags == "object") ? flags : {};
  26. if (typeof flags.allowCC != "boolean") {
  27. flags.allowCC = true;
  28. }
  29. if (typeof flags.allowInfra != "boolean") {
  30. flags.allowInfra = true;
  31. }
  32. if (typeof flags.allowGeneric != "boolean") {
  33. flags.allowGeneric = true;
  34. }
  35. // Infrastructure top-level domain - only one at present
  36. var infraRE = "arpa";
  37. // Generic top-level domains RE.
  38. var genericRE = "aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|xxx|jobs|mobi|post";
  39. // Country Code top-level domains RE
  40. var ccRE = "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|"
  41. + "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|"
  42. + "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|"
  43. + "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|"
  44. + "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|"
  45. + "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|"
  46. + "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|"
  47. + "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";
  48. // Build top-level domain RE
  49. var a = [];
  50. if (flags.allowInfra) {
  51. a.push(infraRE);
  52. }
  53. if (flags.allowGeneric) {
  54. a.push(genericRE);
  55. }
  56. if (flags.allowCC) {
  57. a.push(ccRE);
  58. }
  59. var tldRE = "";
  60. if (a.length > 0) {
  61. tldRE = "(" + a.join("|") + ")";
  62. }
  63. return tldRE; // String
  64. }
  65. dojox.regexp.ipAddress = function(/* Object? */flags) {
  66. // summary: Builds a RE that matches an IP Address
  67. //
  68. // description:
  69. // Supports 5 formats for IPv4: dotted decimal, dotted hex, dotted
  70. // octal, decimal and hexadecimal.
  71. // Supports 2 formats for Ipv6.
  72. //
  73. // flags An object. All flags are boolean with default = true.
  74. // flags.allowDottedDecimal Example, 207.142.131.235. No zero padding.
  75. // flags.allowDottedHex Example, 0x18.0x11.0x9b.0x28. Case insensitive.
  76. // Zero padding allowed.
  77. // flags.allowDottedOctal Example, 0030.0021.0233.0050. Zero padding
  78. // allowed.
  79. // flags.allowDecimal Example, 3482223595. A decimal number between
  80. // 0-4294967295.
  81. // flags.allowHex Example, 0xCF8E83EB. Hexadecimal number between
  82. // 0x0-0xFFFFFFFF.
  83. // Case insensitive. Zero padding allowed.
  84. // flags.allowIPv6 IPv6 address written as eight groups of four
  85. // hexadecimal digits.
  86. // FIXME: ipv6 can be written multiple ways IIRC
  87. // flags.allowHybrid IPv6 address written as six groups of four
  88. // hexadecimal digits
  89. // followed by the usual 4 dotted decimal digit notation of IPv4.
  90. // x:x:x:x:x:x:d.d.d.d
  91. // assign default values to missing paramters
  92. flags = (typeof flags == "object") ? flags : {};
  93. if (typeof flags.allowDottedDecimal != "boolean") {
  94. flags.allowDottedDecimal = true;
  95. }
  96. if (typeof flags.allowDottedHex != "boolean") {
  97. flags.allowDottedHex = true;
  98. }
  99. if (typeof flags.allowDottedOctal != "boolean") {
  100. flags.allowDottedOctal = true;
  101. }
  102. if (typeof flags.allowDecimal != "boolean") {
  103. flags.allowDecimal = true;
  104. }
  105. if (typeof flags.allowHex != "boolean") {
  106. flags.allowHex = true;
  107. }
  108. if (typeof flags.allowIPv6 != "boolean") {
  109. flags.allowIPv6 = true;
  110. }
  111. if (typeof flags.allowHybrid != "boolean") {
  112. flags.allowHybrid = true;
  113. }
  114. // decimal-dotted IP address RE.
  115. var dottedDecimalRE =
  116. // Each number is between 0-255. Zero padding is not allowed.
  117. "((\\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])";
  118. // dotted hex IP address RE. Each number is between 0x0-0xff. Zero
  119. // padding is allowed, e.g. 0x00.
  120. var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]";
  121. // dotted octal IP address RE. Each number is between 0000-0377.
  122. // Zero padding is allowed, but each number must have at least 4
  123. // characters.
  124. var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]";
  125. // decimal IP address RE. A decimal number between 0-4294967295.
  126. 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}|"
  127. + "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])";
  128. // hexadecimal IP address RE.
  129. // A hexadecimal number between 0x0-0xFFFFFFFF. Case insensitive. Zero
  130. // padding is allowed.
  131. var hexRE = "0[xX]0*[\\da-fA-F]{1,8}";
  132. // IPv6 address RE.
  133. // The format is written as eight groups of four hexadecimal digits,
  134. // x:x:x:x:x:x:x:x,
  135. // where x is between 0000-ffff. Zero padding is optional. Case
  136. // insensitive.
  137. var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}";
  138. // IPv6/IPv4 Hybrid address RE.
  139. // The format is written as six groups of four hexadecimal digits,
  140. // followed by the 4 dotted decimal IPv4 format. x:x:x:x:x:x:d.d.d.d
  141. var hybridRE = "([\\da-fA-F]{1,4}\\:){6}"
  142. + "((\\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])";
  143. // Build IP Address RE
  144. var a = [];
  145. if (flags.allowDottedDecimal) {
  146. a.push(dottedDecimalRE);
  147. }
  148. if (flags.allowDottedHex) {
  149. a.push(dottedHexRE);
  150. }
  151. if (flags.allowDottedOctal) {
  152. a.push(dottedOctalRE);
  153. }
  154. if (flags.allowDecimal) {
  155. a.push(decimalRE);
  156. }
  157. if (flags.allowHex) {
  158. a.push(hexRE);
  159. }
  160. if (flags.allowIPv6) {
  161. a.push(ipv6RE);
  162. }
  163. if (flags.allowHybrid) {
  164. a.push(hybridRE);
  165. }
  166. var ipAddressRE = "";
  167. if (a.length > 0) {
  168. ipAddressRE = "(" + a.join("|") + ")";
  169. }
  170. return ipAddressRE; // String
  171. }
  172. dojox.regexp.host = function(/* Object? */flags) {
  173. // summary: Builds a RE that matches a host
  174. // description: A host is a domain name or an IP address, possibly
  175. // followed by a port number.
  176. // flags: An object.
  177. // flags.allowIP Allow an IP address for hostname. Default is true.
  178. // flags.allowLocal Allow the host to be "localhost". Default is false.
  179. // flags.allowPort Allow a port number to be present. Default is true.
  180. // flags in regexp.ipAddress can be applied.
  181. // flags in regexp.tld can be applied.
  182. // assign default values to missing paramters
  183. flags = (typeof flags == "object") ? flags : {};
  184. if (typeof flags.allowIP != "boolean") {
  185. flags.allowIP = true;
  186. }
  187. if (typeof flags.allowLocal != "boolean") {
  188. flags.allowLocal = false;
  189. }
  190. if (typeof flags.allowPort != "boolean") {
  191. flags.allowPort = true;
  192. }
  193. // Domain names can not end with a dash.
  194. var domainNameRE = "([0-9a-zA-Z]([-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?\\.)+"
  195. + dojox.regexp.tld(flags);
  196. // port number RE
  197. var portRE = (flags.allowPort) ? "(\\:" + dojox.regexp.integer({
  198. signed : false
  199. }) + ")?" : "";
  200. // build host RE
  201. var hostNameRE = domainNameRE;
  202. if (flags.allowIP) {
  203. hostNameRE += "|" + dojox.regexp.ipAddress(flags);
  204. }
  205. if (flags.allowLocal) {
  206. hostNameRE += "|localhost";
  207. }
  208. return "(" + hostNameRE + ")" + portRE; // String
  209. }
  210. dojox.regexp.url = function(/* Object? */flags) {
  211. // summary: Builds a regular expression that matches a URL
  212. //
  213. // flags: An object
  214. // flags.scheme Can be true, false, or [true, false].
  215. // This means: required, not allowed, or match either one.
  216. // flags in regexp.host can be applied.
  217. // flags in regexp.ipAddress can be applied.
  218. // flags in regexp.tld can be applied.
  219. // assign default values to missing paramters
  220. flags = (typeof flags == "object") ? flags : {};
  221. if (typeof flags.scheme == "undefined") {
  222. flags.scheme = [true, false];
  223. }
  224. // Scheme RE
  225. var protocolRE = dojo.regexp.buildGroupRE(flags.scheme, function(q) {
  226. if (q) {
  227. return "(https?|ftps?)\\://";
  228. }
  229. return "";
  230. });
  231. // Path and query and anchor RE
  232. var pathRE = "(/([^?#\\s/]+/)*)?([^?#\\s/]+(\\?[^?#\\s/]*)?(#[A-Za-z][\\w.:-]*)?)?";
  233. return protocolRE + dojox.regexp.host(flags) + pathRE;
  234. }
  235. dojox.regexp.emailAddress = function(/* Object? */flags) {
  236. // summary: Builds a regular expression that matches an email address
  237. //
  238. // flags: An object
  239. // flags.allowCruft Allow address like <mailto:foo@yahoo.com>. Default
  240. // is false.
  241. // flags in regexp.host can be applied.
  242. // flags in regexp.ipAddress can be applied.
  243. // flags in regexp.tld can be applied.
  244. // assign default values to missing paramters
  245. flags = (typeof flags == "object") ? flags : {};
  246. if (typeof flags.allowCruft != "boolean") {
  247. flags.allowCruft = false;
  248. }
  249. flags.allowPort = false; // invalid in email addresses
  250. // user name RE - apostrophes are valid if there's not 2 in a row
  251. var usernameRE = "([\\da-zA-Z]+[-._+&'])*[\\da-zA-Z]+";
  252. // build emailAddress RE
  253. var emailAddressRE = usernameRE + "@" + dojox.regexp.host(flags);
  254. // Allow email addresses with cruft
  255. if (flags.allowCruft) {
  256. emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
  257. }
  258. return emailAddressRE; // String
  259. }
  260. dojox.regexp.emailAddressList = function(/* Object? */flags) {
  261. // summary: Builds a regular expression that matches a list of email
  262. // addresses.
  263. //
  264. // flags: An object.
  265. // flags.listSeparator The character used to separate email addresses.
  266. // Default is ";", ",", "\n" or " ".
  267. // flags in regexp.emailAddress can be applied.
  268. // flags in regexp.host can be applied.
  269. // flags in regexp.ipAddress can be applied.
  270. // flags in regexp.tld can be applied.
  271. // assign default values to missing paramters
  272. flags = (typeof flags == "object") ? flags : {};
  273. if (typeof flags.listSeparator != "string") {
  274. flags.listSeparator = "\\s;,";
  275. }
  276. // build a RE for an Email Address List
  277. var emailAddressRE = dojox.regexp.emailAddress(flags);
  278. var emailAddressListRE = "(" + emailAddressRE + "\\s*["
  279. + flags.listSeparator + "]\\s*)*" + emailAddressRE + "\\s*["
  280. + flags.listSeparator + "]?\\s*";
  281. return emailAddressListRE; // String
  282. }
  283. dojox.regexp.us.state = function(/* Object? */flags) {
  284. // summary: A regular expression to match US state and territory
  285. // abbreviations
  286. //
  287. // flags An object.
  288. // flags.allowTerritories Allow Guam, Puerto Rico, etc. Default is true.
  289. // flags.allowMilitary Allow military 'states', e.g. Armed Forces Europe
  290. // (AE). Default is true.
  291. // assign default values to missing paramters
  292. flags = (typeof flags == "object") ? flags : {};
  293. if (typeof flags.allowTerritories != "boolean") {
  294. flags.allowTerritories = true;
  295. }
  296. if (typeof flags.allowMilitary != "boolean") {
  297. flags.allowMilitary = true;
  298. }
  299. // state RE
  300. var statesRE = "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|"
  301. + "NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
  302. // territories RE
  303. var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI";
  304. // military states RE
  305. var militaryRE = "AA|AE|AP";
  306. // Build states and territories RE
  307. if (flags.allowTerritories) {
  308. statesRE += "|" + territoriesRE;
  309. }
  310. if (flags.allowMilitary) {
  311. statesRE += "|" + militaryRE;
  312. }
  313. return "(" + statesRE + ")"; // String
  314. }
  315. dojox.regexp.ca.postalCode = function() {
  316. var postalRE = "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]";
  317. return "(" + postalRE + ")";
  318. }
  319. dojox.regexp.ca.province = function() {
  320. // summary: a regular expression to match Canadian Province
  321. // Abbreviations
  322. var stateRE = "AB|BC|MB|NB|NL|NS|NT|NU|ON|PE|QC|SK|YT";
  323. return "(" + statesRE + ")";
  324. }
  325. dojox.regexp.numberFormat = function(/* Object? */flags) {
  326. // summary: Builds a regular expression to match any sort of number
  327. // based format
  328. // description:
  329. // Use this method for phone numbers, social security numbers,
  330. // zip-codes, etc.
  331. // The RE can match one format or one of multiple formats.
  332. //
  333. // Format
  334. // # Stands for a digit, 0-9.
  335. // ? Stands for an optional digit, 0-9 or nothing.
  336. // All other characters must appear literally in the expression.
  337. //
  338. // Example
  339. // "(###) ###-####" -> (510) 542-9742
  340. // "(###) ###-#### x#???" -> (510) 542-9742 x153
  341. // "###-##-####" -> 506-82-1089 i.e. social security number
  342. // "#####-####" -> 98225-1649 i.e. zip code
  343. //
  344. // flags: An object
  345. // flags.format A string or an Array of strings for multiple formats.
  346. // assign default values to missing paramters
  347. flags = (typeof flags == "object") ? flags : {};
  348. if (typeof flags.format == "undefined") {
  349. flags.format = "###-###-####";
  350. }
  351. // Converts a number format to RE.
  352. var digitRE = function(format) {
  353. // escape all special characters, except '?'
  354. format = dojo.regexp.escapeString(format, "?");
  355. // Now replace '?' with Regular Expression
  356. format = format.replace(/\?/g, "\\d?");
  357. // replace # with Regular Expression
  358. format = format.replace(/#/g, "\\d");
  359. return format; // String
  360. };
  361. // build RE for multiple number formats
  362. return dojo.regexp.buildGroupRE(flags.format, digitRE); // String
  363. }
  364. }