source: Dev/trunk/src/client/dojox/validate/_base.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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