Last change
on this file since 529 was
483,
checked in by hendrikvanantwerpen, 11 years ago
|
Added Dojo 1.9.3 release.
|
File size:
968 bytes
|
Line | |
---|
1 | define(["dojo/_base/lang", "./_base"], function(lang, validate){ |
---|
2 | |
---|
3 | validate.isValidIsbn = function(/* String */value) { |
---|
4 | // summary: |
---|
5 | // Validate ISBN-10 or ISBN-13 based on the length of value |
---|
6 | // value: String |
---|
7 | // An ISBN to validate |
---|
8 | // returns: Boolean |
---|
9 | var len, sum = 0, weight; |
---|
10 | if(!lang.isString(value)){ |
---|
11 | value = String(value); |
---|
12 | } |
---|
13 | value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces |
---|
14 | len = value.length; |
---|
15 | |
---|
16 | switch(len){ |
---|
17 | case 10: |
---|
18 | weight = len; |
---|
19 | // ISBN-10 validation algorithm |
---|
20 | for(var i = 0; i < 9; i++){ |
---|
21 | sum += parseInt(value.charAt(i)) * weight; |
---|
22 | weight--; |
---|
23 | } |
---|
24 | var t = value.charAt(9).toUpperCase(); |
---|
25 | sum += t == 'X' ? 10 : parseInt(t); |
---|
26 | return sum % 11 == 0; // Boolean |
---|
27 | break; |
---|
28 | case 13: |
---|
29 | weight = -1; |
---|
30 | for(var i = 0; i< len; i++){ |
---|
31 | sum += parseInt(value.charAt(i)) * (2 + weight); |
---|
32 | weight *= -1; |
---|
33 | } |
---|
34 | return sum % 10 == 0; // Boolean |
---|
35 | break; |
---|
36 | } |
---|
37 | return false; |
---|
38 | }; |
---|
39 | |
---|
40 | return validate.isValidIsbn; |
---|
41 | }); |
---|
Note: See
TracBrowser
for help on using the repository browser.