Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

php - checking a password for upper lower numbers and symbols

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 
    }     
?> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

your desired regex is below

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

   preg_match($pattern,$password);

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...