Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To 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 November 11th, 2004, 05:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default Regular Expression Help

Dear All,
I need to create a regex to validate a string to ensure that each character is either a letter, number of from a list of special characters in any order in the string. No other characters should be allowed.

Can anyone help ?
__________________
All the best

Greg
------------------
Greg Griffiths
Web & Livelink Developer
http://www.greggriffiths.org
  #2 (permalink)  
Old November 11th, 2004, 06:52 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hey Greg,

I'm thinking it should look something like this:

[[A-Za-z0-9](\*|&|\(|\-)]+

or

([A-Za-z0-9](\*|&|\(|\-))+

These patterns should match any letter or digit, as well as *, &, (, and - (customize these for your characters).

But I'm new to regex's, maybe these are incorrect.

-Snib
Where will you be in 100 years?
Try new FreshView 0.2!
  #3 (permalink)  
Old November 11th, 2004, 07:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

Snib,
thanks a lot for the reply, although that resolves the checking issue, it does not handle the check to ensure that at least one of each type - character, number & special char - are in the string.
  #4 (permalink)  
Old November 12th, 2004, 04:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think you just need all your allowed characters (for example %@!) in brackets with anchors:
Code:
^[\w\d%@!]+$


--

Joe (Microsoft MVP - XML)
  #5 (permalink)  
Old November 12th, 2004, 05:33 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Greg,

If I understand you right, you want at least one of each of the digit group, alpha group and special characters in any order.
After playing about a bit, I came up with...
Code:
^([0-9]+(([a-zA-Z]+[0-9]*[£])|([£]+[0-9]*[a-zA-Z]))|[a-zA-Z]+(([0-9]+[a-zA-Z]*[£])|([£]+[a-zA-Z]*[0-9]))|[£]+(([0-9]+[£]*[a-zA-Z])|([a-zA-Z]+[£]*[0-9])))[0-9a-zA-Z£]*$
which I think does what you're after (I used £ as a special character), not much fun to construct though, so I created the pattern using this...
Code:
function GetExp(specialChars){
    var digits = "0-9";
    var alphas = "a-zA-Z";
    var temp = "^(";
    temp += GetExpPart(digits, alphas, specialChars) + "|";
    temp += GetExpPart(alphas, digits, specialChars) + "|";
    temp += GetExpPart(specialChars, digits, alphas);
    temp += ")[" + digits + alphas + specialChars + "]*$"
    return temp;
}
function GetExpPart(pFirstGroup, pOtherGroup1, pOtherGroup2){
    return "[" + pFirstGroup + "]+(([" + pOtherGroup1 + "]+[" + pFirstGroup + "]*[" + pOtherGroup2 + "])" + 
            "|([" + pOtherGroup2 + "]+[" + pFirstGroup + "]*[" + pOtherGroup1 + "]))";
}
HTH,

Chris




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 if-then smay Javascript 4 July 5th, 2005 12:10 PM
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.