Solved

ISBN Regular Expression

Posted on 2004-08-09
4
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

729 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