Solved

Regex Help c#

Posted on 2014-03-07
8
377 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 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

732 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