Jump to content

Regex Issue


Paradoxz

Recommended Posts

Yes, Regex.. everyone's most feared topic. I hope some brave soul can help.

 

Trying for password validation, numbers and letters only allowed and 8-12 characters. I have tried hundreds of combinations, the one below is the closest I have gotten. The issue is anything under 8 doesn't work but everything over 12 still does for some reason.

 

/(\d|[a-z]){8,12}/

 

also tried: (same results)

 

/\d|[a-z]{8,12}/

 

 

Anyone know why the 12 isn't registering?

 

 


// password validator
$passpattern = "/(\d|[a-z]){8,12}/";
if(!preg_match($passpattern, $_POST['pass'])) { 

		header("location:add.php?errorcode=5");
		die('');

}


Link to comment
Share on other sites

<?php

$Pattern = '/^[a-z0-9]{8,12}$/';

?>

 

Ah yes, the ^ begins with and $ ends with. It works now that they are added, how dumb of me to forget them.

 

 

/^(\d|[a-z]){8,12}$/          is what I used.

 

it's the same as you wrote, except \d is identical to 0-9 I guess having to throw | in there makes it the same amount of characters though, and probably the same speed.

 

 

thank you.

Link to comment
Share on other sites

Try instead

$pattern = '/^([a-zA-Z0-9]{8,12})$/i';

 

Fugix,

 

great advice on the /i

 

Not sure why, but on my email validator and password validator I don't use /i and I only specify a-z not a-zA-Z and it works fine for upper or lower case, so I always omit the /i and A-Z

Link to comment
Share on other sites

If you add the end of line modifier then it will still match anything above 12.. if my pass word is abcdefghijklmnop it will only match the last 12 characters... efghijklmnop

 

try:

 

if(!preg_match($passpattern, $_POST['pass']) && strlen($_POST['pass'])>=12) {

Link to comment
Share on other sites

If you add the end of line modifier then it will still match anything above 12.. if my pass word is abcdefghijklmnop it will only match the last 12 characters... efghijklmnop

 

try:

 

if(!preg_match($passpattern, $_POST['pass']) && strlen($_POST['pass'])<=12) {

 

 

The end of line modifier is working great and limiting it at 12. See code below, you can also try it here: http://failbet.com/acct/add.php

// password validator
$passpattern = "/^(\d|[a-z]){8,12}$/";
if(!preg_match($passpattern, $_POST['pass'])) { 

		header("location:add.php?errorcode=5");
		die('');

}

 

Thanks again everyone!

Link to comment
Share on other sites

You don't really even need a pattern to validate that.

 

if( ctype_alnum($password) && strlen($password) > 7 && strlen($password) < 13 ) {
     // meets criteria
}

 

Excellent script as well, I am trying to get better at Regex (hence the more complicated approach I took)  :) Thanks for your help!

Link to comment
Share on other sites

Sort of off topic, but why on earth are you restricting user passwords?

 

Enforce minimum length, enforce at least one upper, lower, digit, symbol, etc... but you should never have a maximum length or disallow characters.

 

I like the RegEx-less solution, but the cleanest RegEx will be

 

/^[a-z\d]{8,12}+$/i

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.