web.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.web"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.validate.web"] = true;
  8. dojo.provide("dojox.validate.web");
  9. dojo.require("dojox.validate._base");
  10. dojox.validate.isIpAddress = function(/*String*/value, /*Object?*/flags) {
  11. // summary: Validates an IP address
  12. //
  13. // description:
  14. // Supports 5 formats for IPv4: dotted decimal, dotted hex, dotted octal, decimal and hexadecimal.
  15. // Supports 2 formats for Ipv6.
  16. //
  17. // value A string.
  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. // flags.allowHybrid IPv6 address written as six groups of four hexadecimal digits
  27. // followed by the usual 4 dotted decimal digit notation of IPv4. x:x:x:x:x:x:d.d.d.d
  28. var re = new RegExp("^" + dojox.validate.regexp.ipAddress(flags) + "$", "i");
  29. return re.test(value); // Boolean
  30. }
  31. dojox.validate.isUrl = function(/*String*/value, /*Object?*/flags) {
  32. // summary: Checks if a string could be a valid URL
  33. // value: A string
  34. // flags: An object
  35. // flags.scheme Can be true, false, or [true, false].
  36. // This means: required, not allowed, or either.
  37. // flags in regexp.host can be applied.
  38. // flags in regexp.ipAddress can be applied.
  39. // flags in regexp.tld can be applied.
  40. var re = new RegExp("^" + dojox.validate.regexp.url(flags) + "$", "i");
  41. return re.test(value); // Boolean
  42. }
  43. dojox.validate.isEmailAddress = function(/*String*/value, /*Object?*/flags) {
  44. // summary: Checks if a string could be a valid email address
  45. //
  46. // value: A string
  47. // flags: An object
  48. // flags.allowCruft Allow address like <mailto:foo@yahoo.com>. Default is false.
  49. // flags in regexp.host can be applied.
  50. // flags in regexp.ipAddress can be applied.
  51. // flags in regexp.tld can be applied.
  52. var re = new RegExp("^" + dojox.validate.regexp.emailAddress(flags) + "$", "i");
  53. return re.test(value); // Boolean
  54. }
  55. dojox.validate.isEmailAddressList = function(/*String*/value, /*Object?*/flags) {
  56. // summary: Checks if a string could be a valid email address list.
  57. //
  58. // value A string.
  59. // flags An object.
  60. // flags.listSeparator The character used to separate email addresses. Default is ";", ",", "\n" or " ".
  61. // flags in regexp.emailAddress can be applied.
  62. // flags in regexp.host can be applied.
  63. // flags in regexp.ipAddress can be applied.
  64. // flags in regexp.tld can be applied.
  65. var re = new RegExp("^" + dojox.validate.regexp.emailAddressList(flags) + "$", "i");
  66. return re.test(value); // Boolean
  67. }
  68. dojox.validate.getEmailAddressList = function(/*String*/value, /*Object?*/flags) {
  69. // summary: Check if value is an email address list. If an empty list
  70. // is returned, the value didn't pass the test or it was empty.
  71. //
  72. // value: A string
  73. // flags: An object (same as dojo.validate.isEmailAddressList)
  74. if(!flags) { flags = {}; }
  75. if(!flags.listSeparator) { flags.listSeparator = "\\s;,"; }
  76. if ( dojox.validate.isEmailAddressList(value, flags) ) {
  77. return value.split(new RegExp("\\s*[" + flags.listSeparator + "]\\s*")); // Array
  78. }
  79. return []; // Array
  80. }
  81. }