source: Dev/branches/rest-dojo-ui/client/dojox/validate/_base.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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