Jump to content

Text Patterns, How Do I Write Them?


White_Lily

Recommended Posts

I am trying to get my registration form secure from a user point of view. What I want is something like a text pattern that won't let the user register unless they have at least 1 number and one capital letter within their password and their password must be at least 6 characters long and maximum of 25 characters long.

 

 

I was wondering how I would go about this, and if their is a text pattern involved, how do I write them?

Link to comment
Share on other sites

I claim no authorship of this Expression. I got it from the public domain at Regular Expression Library, and provided by Steve Smith.

$pattern = '#^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,25}$#'; //modified to handle from 6 to 25 characters.
$password = $_POST['password'];
if(preg_match($pattern,$password)) {
echo 'Passwords good!';
} else {
echo 'Invalid password!';
}

Description:

Password matching expression. Password must be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit.

Link to comment
Share on other sites

Don't set a max length on passwords. The longer they are, the better. You want to promote your users to have as long passwords as possible, and if they get up to 250 characters rejoice!

 

Putting limits on the minimum length, or the minimum level of entropy, however. Now, that's a good thing. People are, unfortunately, way too lax with coming up with proper passwords. Making them all to easy to guess or run a dictionary attack against.

I'd probably even set the minimum length a bit higher, and add some special non alpha-numerical characters into the required list. ;)

Link to comment
Share on other sites

I would just enforce a minimum length, not necessarily any special characters, and suggest that users use a long phrase rather than a password, per se. Using an easy to remember phrase like "My first house was on a mountain top in Brazil" is far more secure than some easy to forget, hard to type, 8 character password, even with special characters.

Link to comment
Share on other sites

That would work if you wanted to disallow my 25-character password with letters, numbers, and special characters.  There's no capital letter in my password, but there are six special characters.  I'm not sure why people think it's so important to make me conform to their password rules.  Just make it 6+ characters and be done with it.  All you're doing is making me write down the password to your site.

Link to comment
Share on other sites

White_Lilly: In case it's not 100% clear by now, that RegExp of yours states that the whole string (password) has to be compromised of digits, upper case letters, or spaces. At least one of each, but the entire password needs to have a width of 0. In short, it matches nothing, but it requires that that nothing has something in it. In other words it creates a paradox, so that it will never match any input its given.

(Not to mention the fact that you escaped the opening bracket of the character group, and missing

the delimiters.)

 

Just use the pattern given to you by jcbones, only drop the 26 part from it.

 

Alternatively, you can use a dual-layer validation RegExp. To allow for simpler password but of greater length, or a short but complex password. Then it'd look like this:

$RegExp = '/(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[.,_!?*\'+\\/&%¤#"§|-]).{6,25}|.{26,}/u';

Note that you don't need to anchor the RegExp to the end or the start, as you want only to check the minimum requirements.

Edited by Christian F.
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.