Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 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 Display Modes
  #1 (permalink)  
Old November 11th, 2004, 04: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
Reply With Quote
  #2 (permalink)  
Old November 11th, 2004, 05: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!
Reply With Quote
  #3 (permalink)  
Old November 11th, 2004, 06: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.
Reply With Quote
  #4 (permalink)  
Old November 12th, 2004, 03: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)
Reply With Quote
  #5 (permalink)  
Old November 12th, 2004, 04: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


Reply With Quote
Reply


Thread Tools
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 06: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 07:30 AM
Regular Expression Help Hadware .NET Web Services 3 November 4th, 2003 09:51 AM



All times are GMT -4. The time now is 12:06 AM.


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