Solved

ISBN Regular Expression

Posted on 2004-08-09
4
657 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now