-2

I am using the jQuery validation plugin. How to validate ISBN number formats in jQuery rule or pattern or Regular Expressions? Is there any Regular Expressions to achieve this? Please share your idea.

jQuery.validator.addMethod("isbnNo", function(value, element) {
    return isValidISBN(value);
}, "Please enter valid ISBN number");

// ISBN Number validationValidation
function isValidISBN(isbn) {
      isbn = isbn.replace(/[^\dX]/gi, '');
      if(isbn.length < 10 || isbn.length > 13){
        return false;
      }
      var chars = isbn.split('');
      if(chars[9].toUpperCase() == 'X'){
        chars[9] = 10;
      }
      var sum = 0;
      for (var i = 0; i < chars.length; i++) {
        sum += ((10-i) * parseInt(chars[i]));
      };
      return ((sum % 11) == 0);
     }
user3091530
  • 620
  • 2
  • 8
  • 22

1 Answers1

1

For validating ISBN number. You can add custom rule to jquery validator like:

jQuery.validator.addMethod("ruleName", function(value, element) {
    return (condition);
}, "Custom Error message");

Then add this to form like:

$('#formId').validate({
    rules : {
        fieldName : { ruleName : true }
    }
});

This http://neilang.com/articles/how-to-check-if-an-isbn-is-valid-in-javascript/ is example code to validate ISBN number.

Manwal
  • 23,450
  • 12
  • 63
  • 93