summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/validate/isbn.js
blob: f40a831a2e87ae9816a0b4919663e0e504f1896c (plain)
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
/*
	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
*/


if(!dojo._hasResource["dojox.validate.isbn"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.validate.isbn"] = true;
dojo.provide("dojox.validate.isbn");
// summary: Provides ISBN validation functions in `dojox.validate`
//
dojox.validate.isValidIsbn = function(/* String */value) {
	// summary: Validate ISBN-10 or ISBN-13 based on the length of value
	// value: String
	//		An ISBN to validate
	// returns: Boolean
	var len, sum = 0, weight;
	if(!dojo.isString(value)){
		value = String(value);
	}
	value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces
	len = value.length;

	switch(len){
		case 10:
			weight = len;
			// ISBN-10 validation algorithm
			for(var i = 0; i < 9; i++){
				sum += parseInt(value.charAt(i)) * weight;
				weight--;
			}
			var t = value.charAt(9).toUpperCase();
			sum += t == 'X' ? 10 : parseInt(t);
			return sum % 11 == 0; // Boolean
			break;
		case 13:
			weight = -1;
			for(var i = 0; i< len; i++){
				sum += parseInt(value.charAt(i)) * (2 + weight);
				weight *= -1;
			}
			return sum % 10 == 0; // Boolean
			break;
	}
	return false;
}

}