SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question Javascript Regex Validation.

    Hello

    I need some help with validating the password strength of my form.

    I am actually looking for a regular expression that could force the users to have at least 2 of the following:

    1. Upper and lower case.
    2. Numbers
    3. Symbols.


    Thanks in advance for any help

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cancer10 View Post
    I am actually looking for a regular expression that could force the users to have at least 2 of the following:

    1. Upper and lower case.
    2. Numbers
    3. Symbols.
    A regular expression won't help you with this. Instead, you will want to set flags for chars/numbers/symbols to false, and loop through each character of the password, setting each flag as is appropriate. After the loop, check to see if all flags are set.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Someone suggested me this solution and I am sharing it with you all

    Code:
    <script>
    function validate(){
    
    	var pw = 'dddDd##';
    
    	if (  ( pw.match(/[a-z]/) && pw.match(/[A-Z]/) ? 1 : 0 ) +
    	     ( pw.match(/\d/) ? 1 : 0 ) +
    	     ( pw.match(/[\`\~\!\@\#\$\%\^\&\*\(\)\_\-\+\=\[{}\\\}\:\;\"\'\<\,\>\.\?\/]/) ? 1 : 0 )
    	     >= 2 ) 
    	{
    		    alert('true');
    	} else {
    		    alert('false');
    	}
    }
    
    </script>
    <input type="button" onclick="validate()" />

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Someone suggested me this solution and I am sharing it with you all
    There's a problem there though. The code is matching a sample password of 'dddDd##' which doesn't contain any digits, but don't you require at least two digits?

    I maintain that looping through the password setting flags for what you require, and afterwards checking that all those flags have been set, is the easiest way to achieve what you require.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No sir

    As I mentioned in my initial post, that atleast 2 of those condition should match.

    In the string 'dddDd##', the 1st and 3rd condition matches which is exactly I am looking for.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cancer10 View Post
    In the string 'dddDd##', the 1st and 3rd condition matches which is exactly I am looking for.
    Then congrats, it seems that you have something which uses multiple regular expressions to achieve your needs.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •