Jessica Posted November 17, 2009 Share Posted November 17, 2009 I want to check that a string contains a letter and a number. I figured the simplest way for ME to do it is check it contains one letter, and then check if it contains a number, and if both are true, it's good. I just still don't get regex. <? print preg_match('/[a-zA-Z]+[0-9]+/', $value); ?> What am I doing wrong? I put "test1" as $value and still get 0? Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/ Share on other sites More sharing options...
Mchl Posted November 17, 2009 Share Posted November 17, 2009 I get 1 from this: $value = 'test1'; print preg_match('/[a-zA-Z]+[0-9]+/', $value); Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959501 Share on other sites More sharing options...
Jessica Posted November 17, 2009 Author Share Posted November 17, 2009 Sorry, what it wasn't matching was: 1test. This is the new expression that matches 1test and test1 and test1test so I think it will work. Can it be simplified any? /([a-zA-Z])+([0-9])+|([0-9])+([a-zA-Z])+/ Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959503 Share on other sites More sharing options...
Mchl Posted November 17, 2009 Share Posted November 17, 2009 Yes. /[a-zA-Z0-9]+/ [edit] Oh wait... you wanted to match at least one letter and at least one number, so scrap that. Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959505 Share on other sites More sharing options...
Jessica Posted November 17, 2009 Author Share Posted November 17, 2009 Right, I know how to check for a letter OR a number, but I want both. (password strenth!) Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959510 Share on other sites More sharing options...
premiso Posted November 17, 2009 Share Posted November 17, 2009 I recall that there is a way, but if you are doing password strength you want to let the user know what is missing, so why not just seperate them out into their own if statements, that way you know what is missing and can inform the user of it so they can properly fix it... I have some code, but it is on a different PC at home, when I get home, if I remember, I will post my code for a password strength checker (it was made for AJAX). Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959513 Share on other sites More sharing options...
Mchl Posted November 17, 2009 Share Posted November 17, 2009 If it's for password strength I would do separate preg_match for letter and another for number. Basing in this, you can send feedback to user telling them to use at least one letter/number Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959514 Share on other sites More sharing options...
Jessica Posted November 17, 2009 Author Share Posted November 17, 2009 The error message just says "Must contain at least one letter and one number" - I think that's clear enough... I might change it later tho if I find it's confusing, thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959519 Share on other sites More sharing options...
Daniel0 Posted November 17, 2009 Share Posted November 17, 2009 To be honest, I would probably just do if (!preg_match('#[0-9]#', $password) || !preg_match('#[a-z]#i', $password)) { echo 'fail'; } Quote Link to comment https://forums.phpfreaks.com/topic/181914-i-still-dont-get-regex-just-want-to-check-a-simple-one/#findComment-959575 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.