regexp.js 12 KB

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