Jump to content

[SOLVED] Preg_match and password criteria fails


spookztar

Recommended Posts

Hi Guys, new to the forums and nice to be here.

 

Yet another poor soul with a regex prob.

 

I can't get the following code to function properly:

          if (!preg_match('/[a-z]+/', $formpassword) AND
              !preg_match('/[A-Z]+/', $formpassword) AND
              !preg_match('/[0-9]+/', $formpassword)) {
              echo "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span>";
              die($loginform);
              }
      elseif (!preg_match('/[a-z]+/', $formusername) AND
              !preg_match('/[A-Z]+/', $formusername) AND
              !preg_match('/[0-9]+/', $formusername)) {
              echo "<span class='warning'>$formusername ERROR: Failed check for required symbols in username. Please adhere to the specifications given.</span>";
              die($loginform);
              }

 

 

It accepts strings such as "aaaaaaaaaa" without complaining and instead returns an error message from further down the scipt saying "username and/or password incorrect", and it's supposed to demand at least one each of uppercase, lowercase and a digit and die right there if it fails. However, If I process an empty form, it reacts with the "required symbols" message just fine.

 

What am I not getting here?

Link to comment
Share on other sites

Merge your regex patterns into one.

$error = null;

if (!preg_match('/^[a-z0-9]+$/i', $formusername))
{
    $error .= "<span class='warning'>$formusername ERROR: Failed check for required symbols in username. Please adhere to the specifications given.</span><br />";
}

if (!preg_match('/^[a-z0-9]+$/i', $formpassword))
{
    $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />";
}

if(!is_null($error))
{
    echo $error;

    die($loginform);
}

Link to comment
Share on other sites

what about this.

$formpassword = "TesT1234";
$valid = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $password)>0)?true:false;
if (!$valid)
{
    $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />";
}

Link to comment
Share on other sites

Thanx for the attempt, but it doesn't nail it. When the name field is processed with "aaaaaaaaaa" and the password field complies it produses no errormessage. when password fails, it produces wrong username.

 

In my own example, replacing the AND's with OR's now gets the job done, but I must say I'm not really too proud about how it's done...

 

Once again, thanx for the effort, I think I'm just gonna marks this one "solved".

Link to comment
Share on other sites

Gezzz

 

OK heres the full example

<?php
$formpassword = "aaaA2aa"; //Works
$formpassword = "aaaaaa"; //Fails
$valid = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword)>0)?true:false;
if (!$valid)
{
    $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />";
}
echo $error; //Added
?>

Link to comment
Share on other sites

I think MadTechie was referring to my code example when he provided you with his code with regards to showing the error message. Perhaps that is why you are getting confused.

 

In my code I changed the way you report errors to the user when their username/password didn't meet your specification. Before you only show'd one error at a time. To be more user friendly I logged all the errors into the error variable and diaplayed all errors in one go.

 

MadTechie's code merged:

<?php

$formusername = "bbbA2bb"; //Works
$formusername = "bbbbbbb"; //Fails


$formpassword = "aaaA2aa"; //Works
$formpassword = "aaaaaa"; //Fails

// set error to a null value
$error = null;

$username_check = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword) > 0) ? true : false;
$password_check = (preg_match('/\A(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S*\z/', $formpassword) > 0) ? true : false;

if (!$username_check)
{
    // set username error message
    $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />";
}

if (!$password_check)
{
    // set password error message
    $error .= "<span class='warning'>$formpassword ERROR: Failed check for required symbols in password. Please adhere to the specifications given.</span><br />";
}

// check that $error is not set to null
// if it is not null we'll display all errors logged
if(!is_null($error))
{
    echo $error;

    die($loginform);
}
else
{
    // success username/password adheres to specification

    // ... rest of code ...
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.