web.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. define("dojox/validate/web", ["./_base", "./regexp"], function(validate, xregexp){
  2. /*=====
  3. validate = dojox.validate;
  4. =====*/
  5. validate.isIpAddress = function(/*String*/value, /*Object?*/flags) {
  6. // summary: Validates 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. // value A string.
  13. // flags An object. All flags are boolean with default = true.
  14. // flags.allowDottedDecimal Example, 207.142.131.235. No zero padding.
  15. // flags.allowDottedHex Example, 0x18.0x11.0x9b.0x28. Case insensitive. Zero padding allowed.
  16. // flags.allowDottedOctal Example, 0030.0021.0233.0050. Zero padding allowed.
  17. // flags.allowDecimal Example, 3482223595. A decimal number between 0-4294967295.
  18. // flags.allowHex Example, 0xCF8E83EB. Hexadecimal number between 0x0-0xFFFFFFFF.
  19. // Case insensitive. Zero padding allowed.
  20. // flags.allowIPv6 IPv6 address written as eight groups of four hexadecimal digits.
  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. var re = new RegExp("^" + xregexp.ipAddress(flags) + "$", "i");
  24. return re.test(value); // Boolean
  25. };
  26. validate.isUrl = function(/*String*/value, /*Object?*/flags) {
  27. // summary: Checks if a string could be a valid URL
  28. // value: A string
  29. // flags: An object
  30. // flags.scheme Can be true, false, or [true, false].
  31. // This means: required, not allowed, or either.
  32. // flags in regexp.host can be applied.
  33. // flags in regexp.ipAddress can be applied.
  34. // flags in regexp.tld can be applied.
  35. var re = new RegExp("^" + xregexp.url(flags) + "$", "i");
  36. return re.test(value); // Boolean
  37. };
  38. validate.isEmailAddress = function(/*String*/value, /*Object?*/flags) {
  39. // summary: Checks if a string could be a valid email address
  40. //
  41. // value: A string
  42. // flags: An object
  43. // flags.allowCruft Allow address like <mailto:foo@yahoo.com>. Default is false.
  44. // flags in regexp.host can be applied.
  45. // flags in regexp.ipAddress can be applied.
  46. // flags in regexp.tld can be applied.
  47. var re = new RegExp("^" + xregexp.emailAddress(flags) + "$", "i");
  48. return re.test(value); // Boolean
  49. };
  50. validate.isEmailAddressList = function(/*String*/value, /*Object?*/flags) {
  51. // summary: Checks if a string could be a valid email address list.
  52. //
  53. // value A string.
  54. // flags An object.
  55. // flags.listSeparator The character used to separate email addresses. Default is ";", ",", "\n" or " ".
  56. // flags in regexp.emailAddress can be applied.
  57. // flags in regexp.host can be applied.
  58. // flags in regexp.ipAddress can be applied.
  59. // flags in regexp.tld can be applied.
  60. var re = new RegExp("^" + xregexp.emailAddressList(flags) + "$", "i");
  61. return re.test(value); // Boolean
  62. };
  63. validate.getEmailAddressList = function(/*String*/value, /*Object?*/flags) {
  64. // summary: Check if value is an email address list. If an empty list
  65. // is returned, the value didn't pass the test or it was empty.
  66. //
  67. // value: A string
  68. // flags: An object (same as dojo.validate.isEmailAddressList)
  69. if(!flags) { flags = {}; }
  70. if(!flags.listSeparator) { flags.listSeparator = "\\s;,"; }
  71. if ( validate.isEmailAddressList(value, flags) ) {
  72. return value.split(new RegExp("\\s*[" + flags.listSeparator + "]\\s*")); // Array
  73. }
  74. return []; // Array
  75. };
  76. return validate;
  77. });