Solved

Regex Help c#

Posted on 2014-03-07
8
366 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 74

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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now