0
$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!

Fayz I
  • 17
  • 8
  • 2
    Too many unnecessary checks. You can validate your password strength in one go, using a single (but complex) `preg_match()` pattern. Look [here](http://stackoverflow.com/questions/5142103/regex-for-password-strength), for instance, or try googling "strong password regex" for more examples. – Sergey Vidusov Feb 18 '16 at 05:17
  • I would go also with lookahead assertions. – zolo Feb 18 '16 at 07:20
  • Yes, but eventually I would like to add custom error msg for each condition. – Fayz I Feb 18 '16 at 17:39
  • ex: uppercase ----> echo; your pass needs at least one upercase , Will this be possible with one single pattern. thanks! – Fayz I Feb 18 '16 at 17:40

3 Answers3

1

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'];
}
Jalpa
  • 697
  • 3
  • 13
1

Try this

$special   = preg_match('/[^a-zA-Z\d]/', $motopass);
naseeba c
  • 1,040
  • 2
  • 14
  • 30
1

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

Sandeep
  • 51
  • 3