Wrox Programmer Forums
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
Old November 11th, 2004, 05:05 PM
Friend of Wrox
Join Date: Jun 2003
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 Griffiths
Web & Livelink Developer
Old November 11th, 2004, 06:52 PM
Friend of Wrox
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts

Hey Greg,

I'm thinking it should look something like this:




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.

Where will you be in 100 years?
Try new FreshView 0.2!
Old November 11th, 2004, 07:44 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post

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.
Old November 12th, 2004, 04:28 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

I think you just need all your allowed characters (for example %@!) in brackets with anchors:


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

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...
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...
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 + "]))";


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.