xProteuSx Posted September 26, 2011 Share Posted September 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
codeprada Posted September 26, 2011 Share Posted September 26, 2011 Use regular expressions. Check out preg_match. Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted September 26, 2011 Author Share Posted September 26, 2011 Yes, I am familiar with preg_match ... however, I was wondering if there is a method of doing this in one step, instead of searching the string for a letter, then searching the same string for a number. Seems messy that way. Any ideas? PS - thank you codeprada Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 26, 2011 Share Posted September 26, 2011 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? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted September 26, 2011 Share Posted September 26, 2011 only letters and numbers? (atleast 5?) /^[a-z0-9]{5,}$/i (without that restriction - atleast one- if you don't want that, change the + to a *) /^[a-z0-9]+$/i Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted September 26, 2011 Author Share Posted September 26, 2011 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. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted September 26, 2011 Share Posted September 26, 2011 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 but whatever works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.