Jump to content

Ensuring string contains letters AND numbers


xProteuSx

Recommended Posts

I'm certain this has been done a hundred times before, but I have been unable to find any information on an efficient way of doing this:

 

I'm creating a membership system, and the password for new users needs to contains both numbers and letters.  How do I go about checking this?  The key is not that it contains only numbers and letters, but that it contains both.  The password cannot be all letters or all numbers.  Know what I mean?

 

;)  Cheers guys and gals.

Link to comment
Share on other sites

This will ensure the password has at least one letter (upper or lower case) and at least 1 number. It can contain any other characters in addition to that validation.

function validPassword($password)
{
    return preg_match("#^.*(?=.*\d)(?=.*[a-z]).*$#i", $password);
}

 

I can't take full credit for this. I modified it from the examples on this page: http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/

 

Amazing what you can find using Google, isn't it?

Link to comment
Share on other sites

mjdamato,

 

Thank you.  That is an excellent link, and I will bookmark it for future reference.  An excellent article, and includes everything I need and some.  Believe me, I Googled this problem, as I do with all of my programming problems.  I guess I was typing in the wrong search criteria.  Problem was that I didn't know that this was referred to as 'regular expression'.  I'm pretty amateur and have no formal schooling in regards to PHP ...

 

RussellReal,

 

Thanks for the help, but the post stipulated that the string needed to be checked for both a letter and a number in it.  Cheers.

Link to comment
Share on other sites

trudat, I didn't read the whole thing, just throwing some more regex out there, the look aheads are pretty cool, but I don't understand why there is a .* after the starting anchor,  I don't believe you need that. if you put both of those look aheads at the very start of the regex, they'd both crawl the string in question, without moving the regex pointer, which means, by the time it gets to the second lookahead, it is back @ the zeroth position, and if either or fail, the regex will fail to match, wordpress kinda makes no sense to me in that password function :P

 

but whatever works :)

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.