phpretard Posted October 16, 2009 Share Posted October 16, 2009 I am trying to check a string for numbers This is what I have: $string="password1"; if(!ereg('[^0-9]', $string)) { $alert="<script>alert('Your Password must contain At Least 1 Number')</script>"; } It doesn't work...any thoughts? Ultimately the password must contain 8 charactors 1 number and 1 letter Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/ Share on other sites More sharing options...
dymon Posted October 16, 2009 Share Posted October 16, 2009 Try this: /[(^0-9)]/ Best regards, Dymon Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938070 Share on other sites More sharing options...
phpretard Posted October 16, 2009 Author Share Posted October 16, 2009 /[(^0-9)]/ thank you for the reply but it didn't work (probably my fault) $string="12345678"; Here is all of it: elseif(strlen($string) < { $alert="<script>alert('Your Password must contain At Least 8 Characters')</script>"; } elseif(!ereg('[^A-Za-z]', $string])) { $alert="<script>alert('Your Password must contain At Least 1 Letter')</script>"; } elseif(!ereg('/[(^0-9)]/', $string)) { $alert="<script>alert('Your Password must contain At Least 1 Number')</script>"; } I am sure there is one string for all of this but I cant figure it... Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938074 Share on other sites More sharing options...
cags Posted October 16, 2009 Share Posted October 16, 2009 ereg is a deprecated function so you should consider using preg_match. To check that a string has at least one number in it... if(!preg_match("~\d~", $input)) { // $input contains no digits. } Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938077 Share on other sites More sharing options...
.josh Posted October 16, 2009 Share Posted October 16, 2009 the problem with your OP code: if(!ereg('[^0-9]', $string)) is that it's a "double negative". You have a negative char class looking for anything NOT 0-9. So ereg will return true if there are no numbers in it. But then you turn around and have a ! in front of it. So basically overall the condition will evaluate true if there IS a number in the string. But anyways, as someone else pointed out, ereg is deprecated. Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938091 Share on other sites More sharing options...
nrg_alpha Posted October 16, 2009 Share Posted October 16, 2009 Hmm.. topic is flaged as solved.. I was going to ask, can the password contain any mix of any 8 characters (aside from requiring 1 letter and 1 number)? In otherwords, would these samples be legal passwords? ..?@!y.6 uÊ7³mÖx¹ but I guess it doesn't matter now Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938119 Share on other sites More sharing options...
salathe Posted October 16, 2009 Share Posted October 16, 2009 (Tangent/)Offtopic: @Crayon Violent: It is a double negative, but your explanation is not correct. In the code described, ereg will return 1 (i.e, true/found) if there is any single non-digit character. Rather than returning true "if there are no numbers in it", it will return true if there is one (or more) non-digit in the string. The "logical not" operator (!) is then used to flip the true/false so the full condition of the if statement will be true if there is not at least one non-digit in the string (ie, true if the value only contains digits). In other words true for 1234567 and false for 12345a. Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938120 Share on other sites More sharing options...
.josh Posted October 16, 2009 Share Posted October 16, 2009 (Tangent/)Offtopic: @Crayon Violent: It is a double negative, but your explanation is not correct. In the code described, ereg will return 1 (i.e, true/found) if there is any single non-digit character. Rather than returning true "if there are no numbers in it", it will return true if there is one (or more) non-digit in the string. The "logical not" operator (!) is then used to flip the true/false so the full condition of the if statement will be true if there is not at least one non-digit in the string (ie, true if the value only contains digits). In other words true for 1234567 and false for 12345a. yeah you're right. that's how I understood it in my head; not very good at translating that to paper though Link to comment https://forums.phpfreaks.com/topic/177918-solved-check-for-numbers/#findComment-938126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.