Solved

ISBN Regular Expression

Posted on 2004-08-09
4
655 Views
Last Modified: 2012-06-21
I've got the following code:

function validateISBN(isbn)
{
      if(isbn.length <= 0)
      {
            return true;
      }
      var expression = new RegExp("(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$");

      if (!expression.test(isbn) )
            return false;      
      else
            return true;
      }

but it doesn't appear to work properly. I've taken the RegExp from some ASP code but I'm still very new at this and was wondering if anyone could see where I was going wrong?

Thanx
0
Comment
Question by:Steven O'Neill
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 11751705
You are wrong with teh escape character back slash.
If you need one escape character then you need to write two of them.
That is only in String.
To avoid the problem creat the RegExp in its short form.
Like this:
 var expression = /(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$/;

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 125 total points
ID: 11751724
In oposite to the short form does the String parameter form looks like this:
 var expression = new RegExp("(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$");

0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 11751773
Zvonko

I'm sure the short form works but I've used thge string parameter form.

Thanx
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11751796
Thanks for points.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now