Ankit Doshi
Ankit Doshi

Reputation: 1174

preg_match to allow specific special characters in string

How can i achieve that password have (backward slash,forward slash,single quote,double quotes) then and then only it will not allow else it will return true using custom validation.i don't have much idea about preg_match so all suggestions are acceptable.

I have tried below code to achieve that but can't get success.

So anyone have some suggestions/idea ?

php code :

    public function check_password($str){
        return ( !preg_match("/^[a-z0-9!@#$%^&*()_-+|;:<>,.?]+$/i", $str)) ? FALSE : TRUE;
    }

if i will put exclude(backward slash,forward slash,single quote,double quotes) then it must allow me to store password in database

For example :

Password  : !@#$%^&*()_-+| ;:<>, // true
Password  : /12345               //false
Password  : 1Aa3!@#$%^&*()_-+| ;:<> //true(here white space also allowed)

Upvotes: 0

Views: 4830

Answers (2)

krasipenkov
krasipenkov

Reputation: 2029

If I correctly understand you want only this characters - backward slash,forward slash,single quote,double quotes to be disabled for you password validation. So here is the code:

 public function check_password($str){
    return ( preg_match('/^[^\\\"\'\/]+$/i', $str));
 }

Upvotes: 1

Mansoor H
Mansoor H

Reputation: 604

Try like this,

public function check_password($str){
 return (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $str)) ? FALSE : TRUE;
} 

Upvotes: 0

Related Questions