Results 1 to 6 of 6

Thread: Javascript Regex Validation.

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    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
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Maybe you could do that, but "at least 2" is a complicated expression. You could have A&B||B&C||A&C, but that would be a lot to write out.
    I would just suggest making three validations, then checking to see if at least 2 were valid. You could even use a single counter variable and see if in the end that is >=2. (For each validation step, add one to the variable.)

    This will be more reliable if you do it serverside, although I suppose if a user wants to avoid your suggested security regulations for passwords, that's not really your problem. (If they turn off Javascript, they can avoid the check.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    cancer10 (11-01-2011)

  4. #3
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    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()" />

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    That's a creative way to do it (although the code looks a little messy, but it's fine).
    One note is that you can do a list of symbols like that, but you might want to do it the other way: check to find a specific character that is NOT a digit and NOT a letter. I'm not sure of the exact regex syntax for that, but it should be a little shorter to code and more accurate. (Of course it is a secondary problem if a user uses a symbol that is not allowed by the system, but you should deal with that serverside and reject the password then.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. The Following User Says Thank You to djr33 For This Useful Post:

    cancer10 (11-01-2011)

  7. #5
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    Thanks for your input sir.

  8. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,190 Times in 3,154 Posts
    Blog Entries
    12

    Default

    A bit more efficient:

    Code:
    <script>
    function validate(){
    	var pw = 'dddDd##';
    	if(
    		( /[a-z]/.test(pw) && /[A-Z]/.test(pw)? 1 : 0 ) +
    		( /\d/.test(pw)? 1 : 0 ) +
    		( /[!-\/:-@[-`{-~]/.test(pw)? 1 : 0 )
    	> 1 ){
    		alert('true');
    	} else {
    		alert('false');
    	}
    }
    </script>
    <input type="button" onclick="validate()" />
    But that (your original criteris) means that:

    #1

    would pass. Perhaps requiring at least 6 characters would be good too:

    Code:
    <script>
    function validate(){
    	var pw = 'dddD##';
    	if(
    		( /[a-z]/.test(pw) && /[A-Z]/.test(pw)? 1 : 0 ) +
    		( /\d/.test(pw)? 1 : 0 ) +
    		( /[!-\/:-@[-`{-~]/.test(pw)? 1 : 0 )
    	> 1 && pw.length > 5){
    		alert('true');
    	} else {
    		alert('false');
    	}
    }
    </script>
    <input type="button" onclick="validate()" />
    Last edited by jscheuer1; 11-01-2011 at 06:43 AM. Reason: left out ~, later - even more condensed, then a caution
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •