Solved

Regex Help c#

Posted on 2014-03-07
8
369 Views
Last Modified: 2014-03-20
Hi all,

I have the following regex expression in the javascript of my page;

/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\w){5,10}/

Open in new window


I want to test a password field to be between 5 - 10 chars, contain at least 1 A-Z, at least 1 a-z, at least 1 digit and at least 1 symbol.

The password pass in javascript using the above regex but I perform a double check server side using the following method;

protected static bool CheckPassword(string password)
    {
        string sPattern = "/^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*\\w){5,10}/";
        return System.Text.RegularExpressions.Regex.IsMatch(password, sPattern);
    }

Open in new window


which is returning false where the javascript one passes?

any idea what im doing wrong here?
0
Comment
Question by:flynny
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39912842
I believe in C# the syntax is:
@"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\w){5,10}"

Open in new window

or at least that's what RegexBuddy says...

HTH,
Dan
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39912977
To add to Dan Craciun's comment, Javascript uses pattern delimiters ( / ... / ) whereas C# does not.

And actually, your pattern appears broken. You have a quantifier ( {5,10} ) that does not follow anything (except a look-ahead). Did you perhaps mean to include a dot before the quantifier?

e.g.

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\w).{5,10}

Open in new window

0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39913193
Actually, the correct syntax is:
"^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@!#$%^&]).{5,10}$"

Open in new window

Without the $ it will only look at the first 10 characters, declaring any string larger than 10 chars as legit.

HTH,
Dan
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39913214
Without the $ it will only look at the first 10 characters, declaring any string larger than 10 chars as legit.
Not quite, but I suspect that the intent was to include the end-of-line anchor.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:flynny
ID: 39917368
Hi guys thanks for this.

Just to clarify the following string;

.{5,10}$

would mean that a string between 5 - 10 chars long would be accepted?

also I notice you changed

(?=.*\w)

to be

(?=.*[@!#$%^&])

if im correct does this not mean that on of the chars in that string is required?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39917384
.{5,10} means repeat the previous group (which is . or any character) 5 to 10 times.

$ means end of line

\w means any word character (letter, digit or underscore) - it won't match special characters

[] means any of the characters enclosed.

(?=.*[@!#$%^&]) means that your string needs to have at least one of the characters between brackets.

HTH,
Dan
0
 

Author Closing Comment

by:flynny
ID: 39941793
perfect,
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39941801
Glad I could help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to truncate multiple tables from SQL Server Database using Stored procedure 8 47
Error in page 3 44
Syntax error 9 45
Error when loading the database 16 54
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now