Solved

Regex Help c#

Posted on 2014-03-07
8
384 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

627 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