summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/validate/isbn.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/dojo-1.7.2/dojox/validate/isbn.js')
-rw-r--r--js/dojo-1.7.2/dojox/validate/isbn.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/js/dojo-1.7.2/dojox/validate/isbn.js b/js/dojo-1.7.2/dojox/validate/isbn.js
new file mode 100644
index 0000000..b6f8b16
--- /dev/null
+++ b/js/dojo-1.7.2/dojox/validate/isbn.js
@@ -0,0 +1,49 @@
+//>>built
+define("dojox/validate/isbn", ["dojo/_base/lang", "./_base"], function(lang, validate){
+// summary: Provides ISBN validation functions in `dojox.validate`
+//
+
+/*=====
+
+ validate = dojox.validate;
+
+=====*/
+
+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(!lang.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;
+};
+
+return validate.isValidIsbn;
+});