6

I am using the below script to check my passwords for length, uppercase, lowercase and numbers.

How could I change it to make it check FOR symbols instead of against symbols?

<?php

    $password = 'afsd323A';
    if( 
        //I want to change this first line so that I am also checking for at least 1 symbol.
            ctype_alnum($password) // numbers & digits only 
        && strlen($password)>6 // at least 7 chars 
        && strlen($password)<21 // at most 20 chars 
        && preg_match('`[A-Z]`',$password) // at least one upper case 
        && preg_match('`[a-z]`',$password) // at least one lower case 
        && preg_match('`[0-9]`',$password) // at least one digit 
        )
    { 
        echo 'valid';

    }
    else
    { 
        echo 'not valid';// not valid 
    }     
?> 
ian
  • 11,605
  • 25
  • 69
  • 96
  • You are checking *for* characters. Can you clarify what you want to do instead? – deceze Apr 07 '11 at 04:58
  • so you want the password to contain at least one symbol as well? – Jacob Apr 07 '11 at 04:58
  • Right Jacob. Upper Lower Numbers and Characters – ian Apr 07 '11 at 05:02
  • Well, the first line is checking for "numbers & digits only", I'd start by removing that... – Kobi Apr 07 '11 at 05:03
  • The problem with using all those function in an if statement, is you are traversing the string at least 4 times, maybe 6 (depending on if php stores the string length). – Jacob Apr 07 '11 at 05:17

4 Answers4

9

your desired regex is below

   $pattern = ' ^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$ ';

   preg_match($pattern,$password);

DEMO

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • Please try to google... here it is [Reference](http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/) – xkeshav Apr 07 '11 at 05:37
  • 1
    I optimized a bit `((?=.*[A-Z])(?=.*[a-z])(?=.*\d).{7,21}) - 27 steps` ; `your - in 50 steps; ` – Dmitrij Golubev Apr 07 '11 at 10:52
  • @Dmitrij how do u find the steps?? any website?? please refer – xkeshav Apr 07 '11 at 11:10
  • I am using RegexBuddy (powerfull and chip tool). Example of debugging you can find [here](http://stackoverflow.com/questions/2348694/how-do-you-debug-a-regex) – Dmitrij Golubev Apr 07 '11 at 11:26
1

Either you determine a list of valid symbols:

preg_match('`[\$\*\.,+\-=@]`',$password)

or you can look for anything that isn't alnum:

preg_match('`[^0-9a-zA-Z]`',$password)
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • Except that OP wants the password to contain at least one of each. At least one upper, one lower, one digit, and one symbol. – Jacob Apr 07 '11 at 05:15
0

Tests if the input consists of 6 or more ASCII characters.The input must contain at least one upper case letter, one lower case letter and one digit.

if(preg_match('/\A(?=[\x20-\x7E]*?[A-Z])(?=[\x20-\x7E]*?[a-z])(?=[\x20-\x7E]*?[0-9])[\x20-\x7E]{6,}\z/' $password))
    echo("valid password");
Craig White
  • 13,492
  • 4
  • 23
  • 36
  • To check if it contains non-alphanumeric characters try this regular expressions: `if(preg_match('/[^a-zA-Z0-9]/', $password) echo('Password contains non-alphanumeric characters');` – Craig White Apr 07 '11 at 05:07
0

I tried with this , working.

$pattern = '/^(?=.*[!@#$%^&*()\-_=+`~\[\]{}?])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,30}$/';
preg_match($pattern,$password);
greg-449
  • 109,219
  • 232
  • 102
  • 145
Asker Ali
  • 11
  • 3