Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 5th, 2005, 10:51 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default regular expression if-then

I'm trying to use a regular expression to validate a multi-line entry of 4 letters followed by 6 numbers. Following that requirement, the user may enter a comma followed by a number, or hit <Enter> to enter a second required value:
abcd000001
abcd000002,1
abcd000003,16
The comma is optional, but if present must be followed by a number.

For the "if comma then match number" I tried this, but I get a syntax error.
  var re = new RegExp(/[A-Za-z]{4}\d{6}\,?(?(1)\d+)/);

Everything through the comma works:
  var re = new RegExp(/[A-Za-z]{4}\d{6}\,?/);
but the if-then part causes the error.

Reply With Quote
  #2 (permalink)  
Old July 5th, 2005, 11:03 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this...
Code:
var re = /[A-Za-z]{4}\d{6}(,\d+)?/;
HTH,

Chris

Reply With Quote
  #3 (permalink)  
Old July 5th, 2005, 11:11 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That works, however "abcd000001,a" causes this if statement to eval to true.

  var re = new RegExp(/[A-Za-z]{4}\d{6}(,\d+)?/);
  if (document.Form1.txt.value.match(re)) {
    return true;
  } else {
    return false;
  }

Reply With Quote
  #4 (permalink)  
Old July 5th, 2005, 11:24 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

You need to add criteria for the start & end of the string as well...
Code:
var ex = /^[A-Za-z]{4}\d{6}(,\d+)?$/;
Reply With Quote
  #5 (permalink)  
Old July 5th, 2005, 12:10 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect. Thanks.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression Help Please rstelma ASP.NET 1.0 and 1.1 Professional 0 January 2nd, 2008 07:01 PM
REGULAR EXPRESSION pallone Javascript How-To 0 September 5th, 2006 06:52 AM
Regular Expression Help Greg Griffiths Javascript How-To 4 November 12th, 2004 05:33 AM
Regular Expression DARSIN General .NET 2 November 9th, 2004 08:30 AM
Regular Expression Help Hadware .NET Web Services 3 November 4th, 2003 10:51 AM



All times are GMT -4. The time now is 10:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.