cacert-testmgr/external/ZendFramework-1.9.5/externals/dojo/dojox/validate/isbn.js
Markus Warg 8398c9048d initially import ZendFramework-1.9.5 into repository
code was modified slightly, so the code differs from the original downloadable 1.9.5 version
2010-03-31 10:12:32 +02:00

38 lines
984 B
JavaScript

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;
}