vinay singh
vinay singh

Reputation: 55

my regex is not working for maximum limit

Hello i have a regex which accepts mostly every character including specials.And i have set it to accept minimum 8 and maximum 30 characters. Everything is right for minimum but it's not working for maximum.

If string is more than 30 or any length. The result is true.

The pattern is here:

 $pattern = '/[A-Za-z0-9' . preg_quote( '.%^&()$#@!/-+/', '/') . ']{8,30}/';

The whole testing code is:

 $pattern = '/^[A-Za-z0-9' . preg_quote( '.%^&()$#@!/-+/', '/') . ']{8,30}$/';

 if(preg_match($pattern, $pass))
   {
     echo '<br>true';
   }
 else
   {
 echo '<br>false';
    }


?>

Upvotes: 0

Views: 447

Answers (2)

ridgerunner
ridgerunner

Reputation: 34435

The first $pattern expression in your question is missing the required: ^ and $ beginning and end of line assertions - (but the example code snippet which follows uses them correctly.)

You also need to escape the dash/hyphen inside the character class - the hyphen defines a range of characters. (Note that the forward slash / is NOT the escape char!) Try this:

$pattern = '/^[A-Za-z0-9.%^&()$#@!\-+\/]{8,30}$/';

Upvotes: 0

Evert
Evert

Reputation: 99816

This will match any string up to 30 characters within the string. You need to include the start and end of the string:

$pattern = '/^[A-Za-z0-9' . preg_quote( '.%^&()$#@!/-+/', '/') . ']{8,30}$/';

Upvotes: 3

Related Questions