Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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.

  #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

  #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;
  }

  #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+)?$/;
  #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.



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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.