Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.validate.common");
14
dojo.require("dojo.regexp");
15
dojo.validate.isText = function (value, flags) {
16
	flags = (typeof flags == "object") ? flags : {};
17
	if (/^\s*$/.test(value)) {
18
		return false;
19
	}
20
	if (typeof flags.length == "number" && flags.length != value.length) {
21
		return false;
22
	}
23
	if (typeof flags.minlength == "number" && flags.minlength > value.length) {
24
		return false;
25
	}
26
	if (typeof flags.maxlength == "number" && flags.maxlength < value.length) {
27
		return false;
28
	}
29
	return true;
30
};
31
dojo.validate.isInteger = function (value, flags) {
32
	var re = new RegExp("^" + dojo.regexp.integer(flags) + "$");
33
	return re.test(value);
34
};
35
dojo.validate.isRealNumber = function (value, flags) {
36
	var re = new RegExp("^" + dojo.regexp.realNumber(flags) + "$");
37
	return re.test(value);
38
};
39
dojo.validate.isCurrency = function (value, flags) {
40
	var re = new RegExp("^" + dojo.regexp.currency(flags) + "$");
41
	return re.test(value);
42
};
43
dojo.validate._isInRangeCache = {};
44
dojo.validate.isInRange = function (value, flags) {
45
	value = value.replace(dojo.lang.has(flags, "separator") ? flags.separator : ",", "", "g").replace(dojo.lang.has(flags, "symbol") ? flags.symbol : "$", "");
46
	if (isNaN(value)) {
47
		return false;
48
	}
49
	flags = (typeof flags == "object") ? flags : {};
50
	var max = (typeof flags.max == "number") ? flags.max : Infinity;
51
	var min = (typeof flags.min == "number") ? flags.min : -Infinity;
52
	var dec = (typeof flags.decimal == "string") ? flags.decimal : ".";
53
	var cache = dojo.validate._isInRangeCache;
54
	var cacheIdx = value + "max" + max + "min" + min + "dec" + dec;
55
	if (typeof cache[cacheIdx] != "undefined") {
56
		return cache[cacheIdx];
57
	}
58
	var pattern = "[^" + dec + "\\deE+-]";
59
	value = value.replace(RegExp(pattern, "g"), "");
60
	value = value.replace(/^([+-]?)(\D*)/, "$1");
61
	value = value.replace(/(\D*)$/, "");
62
	pattern = "(\\d)[" + dec + "](\\d)";
63
	value = value.replace(RegExp(pattern, "g"), "$1.$2");
64
	value = Number(value);
65
	if (value < min || value > max) {
66
		cache[cacheIdx] = false;
67
		return false;
68
	}
69
	cache[cacheIdx] = true;
70
	return true;
71
};
72
dojo.validate.isNumberFormat = function (value, flags) {
73
	var re = new RegExp("^" + dojo.regexp.numberFormat(flags) + "$", "i");
74
	return re.test(value);
75
};
76
dojo.validate.isValidLuhn = function (value) {
77
	var sum, parity, curDigit;
78
	if (typeof value != "string") {
79
		value = String(value);
80
	}
81
	value = value.replace(/[- ]/g, "");
82
	parity = value.length % 2;
83
	sum = 0;
84
	for (var i = 0; i < value.length; i++) {
85
		curDigit = parseInt(value.charAt(i));
86
		if (i % 2 == parity) {
87
			curDigit *= 2;
88
		}
89
		if (curDigit > 9) {
90
			curDigit -= 9;
91
		}
92
		sum += curDigit;
93
	}
94
	return !(sum % 10);
95
};
96