source: Dev/branches/rest-dojo-ui/client/dojox/validate/isbn.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: 1.1 KB
Line 
1define(["dojo/_base/lang", "./_base"], function(lang, validate){
2// summary: Provides ISBN validation functions in `dojox.validate`
3//
4
5/*=====
6
7        validate = dojox.validate;
8
9=====*/
10
11validate.isValidIsbn = function(/* String */value) {
12        // summary: Validate ISBN-10 or ISBN-13 based on the length of value
13        // value: String
14        //              An ISBN to validate
15        // returns: Boolean
16        var len, sum = 0, weight;
17        if(!lang.isString(value)){
18                value = String(value);
19        }
20        value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces
21        len = value.length;
22
23        switch(len){
24                case 10:
25                        weight = len;
26                        // ISBN-10 validation algorithm
27                        for(var i = 0; i < 9; i++){
28                                sum += parseInt(value.charAt(i)) * weight;
29                                weight--;
30                        }
31                        var t = value.charAt(9).toUpperCase();
32                        sum += t == 'X' ? 10 : parseInt(t);
33                        return sum % 11 == 0; // Boolean
34                        break;
35                case 13:
36                        weight = -1;
37                        for(var i = 0; i< len; i++){
38                                sum += parseInt(value.charAt(i)) * (2 + weight);
39                                weight *= -1;
40                        }
41                        return sum % 10 == 0; // Boolean
42                        break;
43        }
44        return false;
45};
46
47return validate.isValidIsbn;
48});
Note: See TracBrowser for help on using the repository browser.