Password check for upper lower numbers and characters

I use the script below to check my passwords for length, uppercase letter, lowercase letters and numbers.

How can I change it so that it changes FOR characters instead of characters?

<?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 
    }     
?> 
+5
source share
3 answers

your desired regular expression is below

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

   preg_match($pattern,$password);

Demo

+7
source

Tests if the input consists of 6 or more ASCII characters. The entry must contain at least one uppercase letter, one lowercase letter and one number.

if(preg_match('/\A(?=[\x20-\x7E]*?[A-Z])(?=[\x20-\x7E]*?[a-z])(?=[\x20-\x7E]*?[0-9])[\x20-\x7E]{6,}\z/' $password))
    echo("valid password");
0
source

Or you define a list of valid characters:

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

or you can search for anything that is not alnum:

preg_match('`[^0-9a-zA-Z]`',$password)
0
source

All Articles