Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Regular expression to check repeating character or digit, check lowercase,uppercase,capital

please help me..i'm stuck in here..

What i actually want is to check the password from repeating single character or digit.

Requirement for repeating

aaaa = false,

abbb = false

abag = false

a33f = false

abcd1234 = true

there is only once for a character should have in password. If more than once repeated, error returns. So hard to explain.

this is my draft code.

1)first i need to check whether the global configuration for repeating character is allowed or not, if yes my password can have repeating char or digit otherwise it would't. After this, i need to check whether the global configuration for lowercase,uppercase or capitals allowed or not.

if($globalCOnf['repeat_pass']=="yes")//allowed
{
   //do nothing
}
else //not allowed
{
   //stuck here :(
   if(preg_match('/(.)\1{1,}/',$user_input_pass)) //only check "aaaa" not "aba"
   {
       echo "change password";
   }
   else 
   {
      if($globalCOnf['having_lower_upper_capital']=="yes")//allowed
      {
        //do nothing
      }
      else
      {
          // can't continue
      }

   }

}

Upvotes: 4

Views: 837

Answers (4)

mickmackusa
mickmackusa

Reputation: 47874

Just allow zero or more characters between two duplicate characters. If there is a match, then the string failed to pass the validation.

Code: (Demo)

$passwords=['aaaa','abbb','abAg','a33f','abcd1234'];
foreach($passwords as $pass){
    echo "$pass: ";
    if(!preg_match('/([a-zA-Z\d]).*\1/',$pass)){
        echo "valid\n";
    }else{
        echo "failed\n";
    }
}

Output:

aaaa: failed
abbb: failed
abAg: valid
a33f: failed
abcd1234: valid

Or as one-line: echo preg_match('/([a-zA-Z\d]).*\1/',$pass)?'failed':'valid'

Using this type of pattern is much more direct that generating a temporary array of characters and counting their occurrences and checking the highest count.

Upvotes: 0

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

Answer for the question.

if($globalCOnf['repeat_pass']=="yes")//allowed
{
  //do nothing
}
else //not allowed
{

  if(max(array_count_values(str_split($user_pass)))>1) 
  {
     echo "change your password now!!!";
  }
  else 
  {
     if($globalCOnf['having_lower_upper_capital']=="yes")//allowed
     {
       //do nothing
     }
     else
     {
         if(preg_match('/[A-Z]/', $user_pass))
         {

            echo "Can't use uppercase";
         }
     }

  }

}

100% working.. :)

Upvotes: 1

Parag Tyagi
Parag Tyagi

Reputation: 8960

Try something like this -

(\d+).*\1

If you get any match there is a repeated character.

Upvotes: 0

You can use array_count_values for this (An alternate regex free solution)

<?php
$password = 'abcdfa';
if(max(array_count_values(str_split($password)))>1)
{
    echo "Choose another password as words you can't have repeatable characters";
}

OUTPUT:

Choose another password as words you can't have repeatable characters

You get that output because a is repeated two times.

Upvotes: 3

Related Questions