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