_base.js 4.5 KB

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