Fayz I
Fayz I

Reputation: 27

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!

Upvotes: 0

Views: 252

Answers (3)

Sandeep
Sandeep

Reputation: 51

'\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.

Upvotes: 1

naseeba c
naseeba c

Reputation: 1050

Try this

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

Upvotes: 1

Jalpa
Jalpa

Reputation: 720

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'];
}

Upvotes: 1

Related Questions