regexp.js 11 KB

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