2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.validate._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.validate._base"] = true;
|
|
|
3 |
dojo.provide("dojox.validate._base");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojo.regexp"); // dojo core expressions
|
|
|
6 |
dojo.require("dojo.number"); // dojo number expressions
|
|
|
7 |
dojo.require("dojox.validate.regexp"); // additional expressions
|
|
|
8 |
|
|
|
9 |
dojox.validate.isText = function(/*String*/value, /*Object?*/flags){
|
|
|
10 |
// summary:
|
|
|
11 |
// Checks if a string has non whitespace characters.
|
|
|
12 |
// Parameters allow you to constrain the length.
|
|
|
13 |
//
|
|
|
14 |
// value: A string
|
|
|
15 |
// flags: {length: Number, minlength: Number, maxlength: Number}
|
|
|
16 |
// flags.length If set, checks if there are exactly flags.length number of characters.
|
|
|
17 |
// flags.minlength If set, checks if there are at least flags.minlength number of characters.
|
|
|
18 |
// flags.maxlength If set, checks if there are at most flags.maxlength number of characters.
|
|
|
19 |
|
|
|
20 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
21 |
|
|
|
22 |
// test for text
|
|
|
23 |
if(/^\s*$/.test(value)){ return false; } // Boolean
|
|
|
24 |
|
|
|
25 |
// length tests
|
|
|
26 |
if(typeof flags.length == "number" && flags.length != value.length){ return false; } // Boolean
|
|
|
27 |
if(typeof flags.minlength == "number" && flags.minlength > value.length){ return false; } // Boolean
|
|
|
28 |
if(typeof flags.maxlength == "number" && flags.maxlength < value.length){ return false; } // Boolean
|
|
|
29 |
|
|
|
30 |
return true; // Boolean
|
|
|
31 |
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
dojox.validate._isInRangeCache = {};
|
|
|
35 |
dojox.validate.isInRange = function(/*String*/value, /*Object?*/flags){
|
|
|
36 |
// summary:
|
|
|
37 |
// Validates whether a string denoting an integer,
|
|
|
38 |
// real number, or monetary value is between a max and min.
|
|
|
39 |
//
|
|
|
40 |
// value: A string
|
|
|
41 |
// flags: {max:Number, min:Number, decimal:String}
|
|
|
42 |
// flags.max A number, which the value must be less than or equal to for the validation to be true.
|
|
|
43 |
// flags.min A number, which the value must be greater than or equal to for the validation to be true.
|
|
|
44 |
// flags.decimal The character used for the decimal point. Default is ".".
|
|
|
45 |
|
|
|
46 |
// fixes ticket #2908
|
|
|
47 |
value = dojo.number.parse(value, flags);
|
|
|
48 |
if(isNaN(value)){
|
|
|
49 |
return false; // Boolean
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// assign default values to missing paramters
|
|
|
53 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
54 |
var max = (typeof flags.max == "number") ? flags.max : Infinity;
|
|
|
55 |
var min = (typeof flags.min == "number") ? flags.min : -Infinity;
|
|
|
56 |
var dec = (typeof flags.decimal == "string") ? flags.decimal : ".";
|
|
|
57 |
|
|
|
58 |
var cache = dojox.validate._isInRangeCache;
|
|
|
59 |
var cacheIdx = value+"max"+max+"min"+min+"dec"+dec;
|
|
|
60 |
if(typeof cache[cacheIdx] != "undefined"){
|
|
|
61 |
return cache[cacheIdx];
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
if ( value < min || value > max ) { cache[cacheIdx] = false; return false; } // Boolean
|
|
|
65 |
|
|
|
66 |
cache[cacheIdx] = true; return true; // Boolean
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
dojox.validate.isNumberFormat = function(/*String*/value, /*Object?*/flags){
|
|
|
70 |
// summary:
|
|
|
71 |
// Validates any sort of number based format
|
|
|
72 |
//
|
|
|
73 |
// description:
|
|
|
74 |
// Use it for phone numbers, social security numbers, zip-codes, etc.
|
|
|
75 |
// The value can be validated against one format or one of multiple formats.
|
|
|
76 |
//
|
|
|
77 |
// Format
|
|
|
78 |
// # Stands for a digit, 0-9.
|
|
|
79 |
// ? Stands for an optional digit, 0-9 or nothing.
|
|
|
80 |
// All other characters must appear literally in the expression.
|
|
|
81 |
//
|
|
|
82 |
// Example
|
|
|
83 |
// "(###) ###-####" -> (510) 542-9742
|
|
|
84 |
// "(###) ###-#### x#???" -> (510) 542-9742 x153
|
|
|
85 |
// "###-##-####" -> 506-82-1089 i.e. social security number
|
|
|
86 |
// "#####-####" -> 98225-1649 i.e. zip code
|
|
|
87 |
//
|
|
|
88 |
// value: A string
|
|
|
89 |
// flags: {format:String}
|
|
|
90 |
// flags.format A string or an Array of strings for multiple formats.
|
|
|
91 |
|
|
|
92 |
var re = new RegExp("^" + dojox.regexp.numberFormat(flags) + "$", "i");
|
|
|
93 |
return re.test(value); // Boolean
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
dojox.validate.isValidLuhn = function(/*String*/value){
|
|
|
97 |
//summary: Compares value against the Luhn algorithm to verify its integrity
|
|
|
98 |
var sum, parity, curDigit;
|
|
|
99 |
if(typeof value!='string'){
|
|
|
100 |
value = String(value);
|
|
|
101 |
}
|
|
|
102 |
value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces
|
|
|
103 |
parity = value.length%2;
|
|
|
104 |
sum=0;
|
|
|
105 |
for(var i=0;i<value.length;i++){
|
|
|
106 |
curDigit = parseInt(value.charAt(i));
|
|
|
107 |
if(i%2==parity){
|
|
|
108 |
curDigit*=2;
|
|
|
109 |
}
|
|
|
110 |
if(curDigit>9){
|
|
|
111 |
curDigit-=9;
|
|
|
112 |
}
|
|
|
113 |
sum+=curDigit;
|
|
|
114 |
}
|
|
|
115 |
return !(sum%10); //Boolean
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
Procedural API Description
|
|
|
120 |
|
|
|
121 |
The main aim is to make input validation expressible in a simple format.
|
|
|
122 |
You define profiles which declare the required and optional fields and any constraints they might have.
|
|
|
123 |
The results are provided as an object that makes it easy to handle missing and invalid input.
|
|
|
124 |
|
|
|
125 |
Usage
|
|
|
126 |
|
|
|
127 |
var results = dojo.validate.check(form, profile);
|
|
|
128 |
|
|
|
129 |
Profile Object
|
|
|
130 |
|
|
|
131 |
var profile = {
|
|
|
132 |
// filters change the field value and are applied before validation.
|
|
|
133 |
trim: ["tx1", "tx2"],
|
|
|
134 |
uppercase: ["tx9"],
|
|
|
135 |
lowercase: ["tx5", "tx6", "tx7"],
|
|
|
136 |
ucfirst: ["tx10"],
|
|
|
137 |
digit: ["tx11"],
|
|
|
138 |
|
|
|
139 |
// required input fields that are blank will be reported missing.
|
|
|
140 |
// required radio button groups and drop-down lists with no selection will be reported missing.
|
|
|
141 |
// checkbox groups and selectboxes can be required to have more than one value selected.
|
|
|
142 |
// List required fields by name and use this notation to require more than one value: {checkboxgroup: 2}, {selectboxname: 3}.
|
|
|
143 |
required: ["tx7", "tx8", "pw1", "ta1", "rb1", "rb2", "cb3", "s1", {"doubledip":2}, {"tripledip":3}],
|
|
|
144 |
|
|
|
145 |
// dependant/conditional fields are required if the target field is present and not blank.
|
|
|
146 |
// At present only textbox, password, and textarea fields are supported.
|
|
|
147 |
dependencies: {
|
|
|
148 |
cc_exp: "cc_no",
|
|
|
149 |
cc_type: "cc_no",
|
|
|
150 |
},
|
|
|
151 |
|
|
|
152 |
// Fields can be validated using any boolean valued function.
|
|
|
153 |
// Use arrays to specify parameters in addition to the field value.
|
|
|
154 |
constraints: {
|
|
|
155 |
field_name1: myValidationFunction,
|
|
|
156 |
field_name2: dojo.validate.isInteger,
|
|
|
157 |
field_name3: [myValidationFunction, additional parameters],
|
|
|
158 |
field_name4: [dojo.validate.isValidDate, "YYYY.MM.DD"],
|
|
|
159 |
field_name5: [dojo.validate.isEmailAddress, false, true],
|
|
|
160 |
},
|
|
|
161 |
|
|
|
162 |
// Confirm is a sort of conditional validation.
|
|
|
163 |
// It associates each field in its property list with another field whose value should be equal.
|
|
|
164 |
// If the values are not equal, the field in the property list is reported as Invalid. Unless the target field is blank.
|
|
|
165 |
confirm: {
|
|
|
166 |
email_confirm: "email",
|
|
|
167 |
pw2: "pw1",
|
|
|
168 |
}
|
|
|
169 |
};
|
|
|
170 |
|
|
|
171 |
Results Object
|
|
|
172 |
|
|
|
173 |
isSuccessful(): Returns true if there were no invalid or missing fields, else it returns false.
|
|
|
174 |
hasMissing(): Returns true if the results contain any missing fields.
|
|
|
175 |
getMissing(): Returns a list of required fields that have values missing.
|
|
|
176 |
isMissing(field): Returns true if the field is required and the value is missing.
|
|
|
177 |
hasInvalid(): Returns true if the results contain fields with invalid data.
|
|
|
178 |
getInvalid(): Returns a list of fields that have invalid values.
|
|
|
179 |
isInvalid(field): Returns true if the field has an invalid value.
|
|
|
180 |
|
|
|
181 |
*/
|
|
|
182 |
|
|
|
183 |
}
|