Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex Help c#

Posted on 2014-03-07
8
Medium Priority
?
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 35

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 35

Accepted Solution

by:
Dan Craciun earned 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 

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 35

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 35

Expert Comment

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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

688 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