Jump to content

[SOLVED] Password length only


gamefreak13

Recommended Posts

I need to make sure the password is between x and y (lets just say 4 and 12) characters long. However, I believe the user should be able to input anything they want for their password, including crazy stuff like "$@1dogsrule1@$". All I care to check is that the that it is between 4 and 12 characters long.

 

I know this would work for letters and numbers, but I don't want to dictate what characters are allowed/disallowed. I tried removing the [a-z0-9] part but it didnt work. Same with removing it plus the ().

 

^([a-z0-9]{4,12})$

 

I know I could use strlen, but that would require two IF statements.. one for "if smaller than 4" and one for "if larger than 12"... so a regex seems like less code which thus seems like a better choice. :)

Link to comment
https://forums.phpfreaks.com/topic/107073-solved-password-length-only/
Share on other sites

I know I could use strlen, but that would require two IF statements.. one for "if smaller than 4" and one for "if larger than 12"... so a regex seems like less code which thus seems like a better choice. :)

 

That's a mistake. Using strlen() is much more efficient than using preg_match().

 

<?php

$len = strlen($pass);
if($len < 4 || $len > 12)
   echo "Bad pass!";

?>

 

Orio.

Thanks.. I set up some strlen's like you said and it works great.

 

In an effort to not make too many threads.. this seems to work but I just wanted to double check to make sure I have it right because I wrote this regex myself. I want "numbers only OR empty cause this field is not required".

 

		if(!preg_match('/^[0-9]+$|^$/', $friendid)) {
		echo "<li>Your FriendID can only contain numbers</li>\n";
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.