简体   繁体   中英

Need to Add regex (php) for at least 1 special char

$uppercase = preg_match('@[A-Z]@', $motpass);
$lowercase = preg_match('@[a-z]@', $motpass);
$number    = preg_match('@[0-9]@', $motpass);
$special   = preg_match('@[@#$%^&+=]@', $motpass);  <---( PROBLEM HERE )

if (!$uppercase || !$lowercase || !$number || !$special || strlen($motpass) < 8)


{

    $motpass_error='NO GOOD';
    $error=true;
}
    else
{
    $motpass = $_POST['motpass'];
}

Im looking for regex (All specials Chararcter or Most )

Thanks in advance!

You can try code like below.

$uppercase = preg_match('@[A-Z]@', $motpass);
$lowercase = preg_match('@[a-z]@', $motpass);
$number    = preg_match('@[0-9]@', $motpass);
$special   = preg_match('/[^a-zA-Z\d]/', $string);

OR

$uppercase = preg_match('@[A-Z]@', $motpass);
$lowercase = preg_match('@[a-z]@', $motpass);
$number    = preg_match('@[0-9]@', $motpass);
$special   = preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$%^&]).*$/');

if (!$uppercase || !$lowercase || !$number || !$special || strlen($motpass) < 8)


{

    $motpass_error='NO GOOD';
    $error=true;
}
    else
{
    $motpass = $_POST['motpass'];
}

尝试这个

$special   = preg_match('/[^a-zA-Z\d]/', $motopass);

'\\w' matches word(alphabet or numeric) character where as '\\W' matches non-word (special characters) character.

So using non-word character makes simple. Try below regex:

$special   = preg_match('/\W+/', $string);

'+' in expression represent one or more word characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM