www.webdeveloper.com
Results 1 to 7 of 7

Thread: Need help with REGEX

  1. #1
    Join Date
    Nov 2010
    Posts
    62

    Need help with REGEX

    Hi everyone.

    I am setting up a password validation script.
    I have spent a while trying to understand how to create my own regular expression but I'm not winning here!

    Could someone help me?

    The requirements are
    Password must contain:

    At least 1 Uppercase Character
    At least 1 Lowercase Character
    At least 1 numeric Character
    The string length must be 8-16.

    Thanks in advance

  2. #2
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    PHP Code:
    <?php

    $req_char 
    TRUE;
    $patterns = array("|[a-z]+|","|[A-Z]+|","|[0-9]+|");
    foreach( 
    $patterns as $pattern ) {
       if( !
    preg_match($pattern,$password) > ) {
          echo 
    $pattern .'failed<br />';
          
    $req_char FALSE;
       }
    }
    if( 
    $req_char && strlen($password) >= && strlen($password) <= 16 ) {
       echo 
    "password is acceptable";
    } else {
       echo 
    "password fails";
    }
    Best I could come up with.

  3. #3
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Found this pattern googling that does the same:
    PHP Code:
    $pattern "((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,16})";
    if( 
    preg_match($pattern,$password) ) {
       echo 
    "Valid Password";
    } else {
       echo 
    "Invalid Password";


  4. #4
    Join Date
    Nov 2010
    Posts
    62
    Thanks I will give that a go soon

  5. #5
    Join Date
    Nov 2010
    Posts
    62
    Hi there.

    it works but my password field is also allowed special character (but they are optional).

    Thanks

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    Quote Originally Posted by pedro93 View Post
    Hi there.

    it works but my password field is also allowed special character (but they are optional).

    Thanks
    Here's a modified version of post #3 to allow for specifying which special characters are allowed:
    PHP Code:
    <?php
    function isValidPwd($password)
    {
       
    /*
        * Change the characters between the [] brackets to desired values
        * hyphen "-" should be last, if used
        * escape "[" or "]" with "\"
        */
       
    $allowedSpecialChars '[\[\]_*%-]';

       
    $allChars "(\w|$allowedSpecialChars)";
       
    $pattern '/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])'.$allChars.'{8,16}$/';
       return (
    preg_match($pattern$password));
    }

    //TEST:
    $passwords = array(
       
    'abCD12-['// valid
       
    'Ab12*-xx'// valid
       
    'Ab!@2345'// invalid special characters
       
    'Ba1-',     // too short
       
    'B1a-xxx_xxxxxxxxxxxxxxxx'// too long
       
    '1axxxx---',// no upper-case
       
    '1Baxxxxx'   // valid
    );
    foreach(
    $passwords as $pwd) {
       
    printf("<p>'%s' is %s</p>\n"$pwd, (isValidPwd($pwd)) ? 'Valid' 'NOT Valid');
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Nov 2010
    Posts
    62
    Hi there. Thanks it worked like a treat.

    Sorry to be a hasstle but you don't suppose you could do it for JS as well?

    here's my current code (I used from else where)
    Code:
    		var pass1 = $("input#password").val();
    		var passregex = /(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/i;
    		if (pass1.search(passregex) == -1) {
    		  $("label#password_error").show(); 
    		  $("input#password").focus();
    		  return false;
    		}
    Thanks a lot. Regex stuff is just far too confusing!
    Cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles