_base.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.validate._base"] = true;
  8. dojo.provide("dojox.validate._base");
  9. dojo.experimental("dojox.validate");
  10. dojo.require("dojo.regexp"); // dojo core expressions
  11. dojo.require("dojo.number"); // dojo number expressions
  12. dojo.require("dojox.validate.regexp"); // additional expressions
  13. dojox.validate.isText = function(/*String*/value, /*Object?*/flags){
  14. // summary:
  15. // Checks if a string has non whitespace characters.
  16. // Parameters allow you to constrain the length.
  17. //
  18. // value: A string
  19. // flags: {length: Number, minlength: Number, maxlength: Number}
  20. // flags.length If set, checks if there are exactly flags.length number of characters.
  21. // flags.minlength If set, checks if there are at least flags.minlength number of characters.
  22. // flags.maxlength If set, checks if there are at most flags.maxlength number of characters.
  23. flags = (typeof flags == "object") ? flags : {};
  24. // test for text
  25. if(/^\s*$/.test(value)){ return false; } // Boolean
  26. // length tests
  27. if(typeof flags.length == "number" && flags.length != value.length){ return false; } // Boolean
  28. if(typeof flags.minlength == "number" && flags.minlength > value.length){ return false; } // Boolean
  29. if(typeof flags.maxlength == "number" && flags.maxlength < value.length){ return false; } // Boolean
  30. return true; // Boolean
  31. }
  32. dojox.validate._isInRangeCache = {};
  33. dojox.validate.isInRange = function(/*String*/value, /*Object?*/flags){
  34. // summary:
  35. // Validates whether a string denoting a number
  36. // is between a max and min.
  37. //
  38. // value: A string
  39. // flags: {max:Number, min:Number, decimal:String}
  40. // flags.max A number, which the value must be less than or equal to for the validation to be true.
  41. // flags.min A number, which the value must be greater than or equal to for the validation to be true.
  42. // flags.decimal The character used for the decimal point. Default is ".".
  43. value = dojo.number.parse(value, flags);
  44. if(isNaN(value)){
  45. return false; // Boolean
  46. }
  47. // assign default values to missing paramters
  48. flags = (typeof flags == "object") ? flags : {};
  49. var max = (typeof flags.max == "number") ? flags.max : Infinity,
  50. min = (typeof flags.min == "number") ? flags.min : -Infinity,
  51. dec = (typeof flags.decimal == "string") ? flags.decimal : ".",
  52. cache = dojox.validate._isInRangeCache,
  53. cacheIdx = value + "max" + max + "min" + min + "dec" + dec
  54. ;
  55. if(typeof cache[cacheIdx] != "undefined"){
  56. return cache[cacheIdx];
  57. }
  58. cache[cacheIdx] = !(value < min || value > max);
  59. return cache[cacheIdx]; // Boolean
  60. }
  61. dojox.validate.isNumberFormat = function(/* String */value, /* Object? */flags){
  62. // summary: Validates any sort of number based format
  63. //
  64. // description:
  65. // Validates any sort of number based format. Use it for phone numbers,
  66. // social security numbers, zip-codes, etc. The value can be validated
  67. // against one format or one of multiple formats.
  68. //
  69. // Format Definition
  70. // | # Stands for a digit, 0-9.
  71. // | ? Stands for an optional digit, 0-9 or nothing.
  72. // All other characters must appear literally in the expression.
  73. //
  74. // example:
  75. // | "(###) ###-####" -> (510) 542-9742
  76. // | "(###) ###-#### x#???" -> (510) 542-9742 x153
  77. // | "###-##-####" -> 506-82-1089 i.e. social security number
  78. // | "#####-####" -> 98225-1649 i.e. zip code
  79. //
  80. // value: A string
  81. //
  82. // flags: Object?
  83. // FIXME: make pseudo-object for this
  84. // format: String
  85. //
  86. // flags.format A string or an Array of strings for multiple formats.
  87. //
  88. // example:
  89. // | // returns true:
  90. // | dojox.validate.isNumberFormat("123-45", { format:"###-##" });
  91. //
  92. // example:
  93. // Check Multiple formats:
  94. // | dojox.validate.isNumberFormat("123-45", {
  95. // | format:["### ##","###-##","## ###"]
  96. // | });
  97. //
  98. var re = new RegExp("^" + dojox.validate.regexp.numberFormat(flags) + "$", "i");
  99. return re.test(value); // Boolean
  100. }
  101. dojox.validate.isValidLuhn = function(/* String */value){
  102. // summary: Validate a String value against the Luhn algorithm.
  103. // description:
  104. // Validate a String value against the Luhn algorithm to verify
  105. // its integrity.
  106. var sum = 0, parity, curDigit;
  107. if(!dojo.isString(value)){
  108. value = String(value);
  109. }
  110. value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces
  111. parity = value.length % 2;
  112. for(var i = 0; i < value.length; i++){
  113. curDigit = parseInt(value.charAt(i));
  114. if(i % 2 == parity){
  115. curDigit *= 2;
  116. }
  117. if(curDigit > 9){
  118. curDigit -= 9;
  119. }
  120. sum += curDigit;
  121. }
  122. return !(sum % 10); // Boolean
  123. }
  124. }