Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

RegEx DataValidation Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bryan H - 08 Feb 2008 17:05 GMT
Hello.. I'm trying to do some realtime password validation.

The requirements are use of at least 3 of 4 datasets (1 upper, 1 lower, 1
number, 1 special char) and a min of 8.

I've got
^.*(?=.{8,})((?=.*\d)|(?=.*[@#$%^&+=]))(?=.*[a-z])(?=.*[A-Z]).*$

that get's me close.. that does min 8, 1 upper, 1 lower and either 1 number
or 1 special from the list
Can anyone help me over the hump of if  4 of the pass, then we're good.
Thanks in advance!
Misbah Arefin - 08 Feb 2008 18:44 GMT
try
^.*(?=.{8,})((?=.*\d)(?=.*[@#\$%\^&\+=]))(?=.*[a-z])(?=.*[A-Z]).*$

Signature

--
Misbah Arefin

> Hello.. I'm trying to do some realtime password validation.
>
[quoted text clipped - 8 lines]
> Can anyone help me over the hump of if  4 of the pass, then we're good.
> Thanks in advance!
Bryan H - 08 Feb 2008 21:25 GMT
Thanks Misbah

That seems to make them all required.

I need at least 3 of the 4 to be met.

> try
> ^.*(?=.{8,})((?=.*\d)(?=.*[@#\$%\^&\+=]))(?=.*[a-z])(?=.*[A-Z]).*$
Misbah Arefin - 08 Feb 2008 22:24 GMT
oops, sorry I misinterpreted your question
try
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.{8,})|(?=.*[a-z])(?=.*[A-Z])(?=.*[@#\$%\^&\+=])(?=.{8,})|(?=.*\d)(?=.*[a-z])(?=.*[@#\$%\^&\+=])(?=.{8,})|(?=.*\d)(?=.*[A-Z])(?=.*[@#\$%\^&\+=])(?=.{8,})).*$

--
Misbah Arefin

> Thanks Misbah
>
[quoted text clipped - 4 lines]
>> try
>> ^.*(?=.{8,})((?=.*\d)(?=.*[@#\$%\^&\+=]))(?=.*[a-z])(?=.*[A-Z]).*$
Bryan H - 11 Feb 2008 14:39 GMT
Perfect..

Thanks a bunch Misbah! I appreciate it.

> oops, sorry I misinterpreted your question
> try
[quoted text clipped - 11 lines]
>>> try
>>> ^.*(?=.{8,})((?=.*\d)(?=.*[@#\$%\^&\+=]))(?=.*[a-z])(?=.*[A-Z]).*$
Jesse Houwing - 13 Feb 2008 15:10 GMT
Hello Bryan,

Even though this expression works, I found it hard to read and probably hard
to maintain, so I've been experimenting with Balanced groups to see if there
was a more elegant solution to this problem. I came up with this expressionL

^(?=(?<valid>.*\W)?)(?=(?<valid>.*[0-9])?)(?=(?<valid>.*[a-z])?).{8,}(?<-valid>){2}$

I'll try to explain how it works:

First notice that the expression looks very similar to what we had before.

If you haven't worked with balancing groups before, which most people haven't
all you need to know is that you can use them to count occurances. Origially
so that you know how many times you for example opened a ( and how many times
you closed one. Instead of ensuring that something that was opened is also
closed I use it here to check if there is at least a specific number of matches
left.

In the above case there are 3 checks done:

- Any special character (\W)
- Any numeric character ([0-9])
- Any lowercase alfanumeric character ([a-z])

As you probably also noted, I made the each of the look aheads optional.
They would otherwise all be enforced, which wouldn't help. Then I used the
above mentioned balancing groups to see how many of these groups actually
did match something. To do so I named each of the tests with (?<valid>....).

You can substract (?-<valid>...) and add (?<valid>....) values matched to
a balancing group. If you try to substract a value when there is no matching
addition, the regex will fail.

With his knowledge we can finish the 2 out of 3 rule we were trying to enforce.

If we substract the least amount of matches we require from the balancing
group without error, the regex will match. If there are not enough matches
left, the regex will fail automatically.

In this example I require 2 out of 3, so 2 substractions must match:

(?<-valid>){2}

Which solves out problem.

Because this all works with named matches you could use this to make pretty
complex rules to which a password must comply. You can also enforce certain
rules at all times, yet have other rules be mutually allowed if at least
x of these optional rules match.

There is one downside though. Clientside javascript does not support balancing
groups, and therefore you can use this regex only at the server side.

it would be nice if the validation controls would allow you to specifiy a
rule on both the client and the server side so that you can do a global match
on the client, and a more thourough one at the server. I think I'll create
my own regex validator to do just that in my next .NET project.

Kind regards,

--
Jesse Houwing
jesse.houwing at sogeti.nl

> Perfect..
>
[quoted text clipped - 20 lines]
>>>> Misbah Arefin
>>>> "Bryan H" <news@psacake.com> wrote in message

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.