Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 6th, 2009, 10:56 AM
Registered User
 
Join Date: Jan 2009
Location: The Netherlands
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default password check with RegEx

Is it possible the following:

Password must contain at least two of the following groups:
- Small letters (a-z)
- Capital letters (A-Z)
- Numbers (0-9)
- Strange charakters (!@#$%^&*_+-/)

I only can find regex's that do and, and, and ...
Hope to here from you all

Bjorn
  #2 (permalink)  
Old January 7th, 2009, 02:06 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I think you could do it, but it would be a LOOOONNNNG regexp.

Something like (in JavaScript syntax)
Code:
/((?=[A-Z])(?=[a-z])|(?=[A-Z])(?=\d)|(?=[a-z])(?=\d)|....)/
That is, you'd have to include every possible pair of your 4 sets of characters...so 6 possible pairs.

Unless you are dead set on doing this all in a single expresssion, it would be easier to just have an "or" of the 4 possibilities and then capture all the matches and ensure that you have at least 2 matches. Depending on what "outside" language you are using, this would be trivial.

Hmmm???
  #3 (permalink)  
Old January 7th, 2009, 02:24 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I couldn't get it to work right using "look ahead" (surely something silly I was doing wrong), but I got it to work by sheer brute force:
Code:
<script>
// there are no spaces or line breaks in the assignment here
// even if the forum software puts some in:
var re = 
/([A-Z][A-Z\d\!\@\#]*[a-z]|[a-z][a-z\d\!\@\#]*[A-Z]|[A-Z][A-Za-z\!\@\#\$\%]*\d|\d[A-Za-z\!\@\#\$\%]*[A-Z]|[a-z][A-Za-z\!\@
\#\$\%]*\d|\d[A-Za-z\!\@\#\$\%]*[a-z]|[a-z][A-Za-z\d]*[\!\@\@]|[\!\@\@][A-Za-z\d]*[a-z]|[A-Z][A-Za-z\d]*[\!\@\@]|[\!\@\@][A-Za-z\d]*[A-Z]|\d[A-Za-z\d]*[\!\@\@]|[\!\@\@][A-Za-z\d]*\d)/

function check(fld)
{
    alert( re.test(fld.value) );
}
</script>
<form>
<input name=foo onChange="check(this);">
<input name=bar onChange="check(this);">
</form>
I only show using 3 of your special characters, to keep it to reasonable size, but you can easily just expand the choices.

Seems to work like a charm.
  #4 (permalink)  
Old January 7th, 2009, 03:52 AM
Registered User
 
Join Date: Jan 2009
Location: The Netherlands
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tafeltennis Vereniging The Victory

Wauw! You did a good job my friend!

Thanks for the excellent example.
  #5 (permalink)  
Old January 7th, 2009, 07:10 AM
Registered User
 
Join Date: Jan 2009
Location: The Netherlands
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Full regex

I have here the compleet regex with all strange charakters.

^(?=.*[A-Z])(?=.*[a-z])[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$|^(?=.*[A-Z])(?=.*\d)[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$|^(?=.*[a-z])(?=.*\d)[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$|^(?=.*[A-Z])(?=.*[!@#$%^&_|\-=+?~(){}:;/.,<>*`'])[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$|^(?=.*[!@#$%^&_|\-=+?~(){}:;/.,<>*`'])(?=.*\d)[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$|^(?=.*[a-z])(?=.*[!@#$%^&_|\-=+?~(){}:;/.,<>*`'])[a-zA-Z\d!@#$%^&_|\-=+?~(){}:;/.,<>*`']{6,}$
  #6 (permalink)  
Old January 7th, 2009, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Just for anybody reading this:

Some RegEx engines will choke on the ( ) in the [...] list. JS or Perl will choke on the / in there. Possible a few other characters in there that could cause problems in various engines. The solution is easy: When in doubt just put \ in front of each possible problem character. Example:
Code:
[a-zA-Z\d!@#$%^&_|\-=+?~\(\)\{\}:;\/.,<>*`']


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check the password field for case-sensitive zubash73 ASP.NET 2.0 Basics 4 November 27th, 2016 02:47 AM
How to check programmatically check the password? thomaskelly ASP.NET 1.0 and 1.1 Basics 1 May 16th, 2008 08:49 PM
Connect to VSS check-in Check-out Programatically rhd110 General .NET 6 August 12th, 2007 07:46 AM
Check Windows-Password Bernd VS.NET 2002/2003 1 October 6th, 2005 12:32 PM
Can't check data from a form. (Like password) scifo Beginning PHP 3 August 7th, 2003 01:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.