bugzy Posted August 9, 2012 Share Posted August 9, 2012 Newbie question guys.. I want to check if a user input/include a number on the textbox? so the requirement is, user must input a string and a number on the textbox and no other conditions just want to check if he/she input a number on it. Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/ Share on other sites More sharing options...
Pikachu2000 Posted August 9, 2012 Share Posted August 9, 2012 Question is too broad. Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368086 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Question is too broad. Pikachu2000 I already edited it already. to make it clear if a user input - "hey you are beautiful" it should return false. if a user input - "hey you are beautiful 2222" it should return true. Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368087 Share on other sites More sharing options...
peipst9lker Posted August 9, 2012 Share Posted August 9, 2012 Probably overkill but yeah... function containsInteger($input) { return (boolean)preg_match("/[0-9]/", $input); } Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368093 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Probably overkill but yeah... function containsInteger($input) { return (boolean)preg_match("/[0-9]/", $input); } Hello, I tried this preg_match("/[0-9]/", $address) == FALSE and this !preg_match("/[0-9]/", $address) But it's not working like... like if I input "Hello World" It's returning a true value even if there's no number included on the input Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368108 Share on other sites More sharing options...
Pikachu2000 Posted August 9, 2012 Share Posted August 9, 2012 Seems to work fine for me. How are you using it? Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368111 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Seems to work fine for me. How are you using it? if(strlen($address) < 20 && !empty($address) && !preg_match("/[0-9]/", $address)) { //invalid } Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368116 Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 Check out jQuery. Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368117 Share on other sites More sharing options...
Pikachu2000 Posted August 9, 2012 Share Posted August 9, 2012 That also works fine when I test it (though it could be written more logically). Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368119 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Check out jQuery. Thanks for that, but I want to focus on server side this time... Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368120 Share on other sites More sharing options...
Pikachu2000 Posted August 9, 2012 Share Posted August 9, 2012 Check out jQuery. Javascript doesn't perform validation. Validation must be performed server-side. Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368121 Share on other sites More sharing options...
jazzman1 Posted August 9, 2012 Share Posted August 9, 2012 As Pika said, it works just fine. Simple example: $input = 'Hello world 2012'; function containsInteger($input) { return (boolean)preg_match("/[0-9]/", $input); } var_dump(containsInteger($input)); Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368123 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 Wow that weird.. Now it's working Anyway thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368126 Share on other sites More sharing options...
Psycho Posted August 9, 2012 Share Posted August 9, 2012 if(strlen($address) < 20 && !empty($address) && !preg_match("/[0-9]/", $address)) { //invalid } As Pikachu stated that could be written more logically and it actually has a flaw. If the user entered the number '0' the validation would fail because the function empty() would return false. But, let's step back a second because that is really overkill. If you must have a check to ensure there is at least 1 number in the value there is no need to do the strlen() or empty() checks at all! Also, I normally do an isset() check on form field values. It could be considered overkill, but I incorporate it with a process to also trim the values which you should be doing anyway. So, here's my take: $address = isset($_POST['address']) ? trim($_POST['address']) : ''; if(!preg_match("/[0-9]/", $address )) { //Invalid echo "The value '$address' is invalid"; //Add this for debugging } Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368127 Share on other sites More sharing options...
bugzy Posted August 9, 2012 Author Share Posted August 9, 2012 if(strlen($address) < 20 && !empty($address) && !preg_match("/[0-9]/", $address)) { //invalid } As Pikachu stated that could be written more logically and it actually has a flaw. If the user entered the number '0' the validation would fail because the function empty() would return false. But, let's step back a second because that is really overkill. If you must have a check to ensure there is at least 1 number in the value there is no need to do the strlen() or empty() checks at all! Also, I normally do an isset() check on form field values. It could be considered overkill, but I incorporate it with a process to also trim the values which you should be doing anyway. So, here's my take: $address = isset($_POST['address']) ? trim($_POST['address']) : ''; if(!preg_match("/[0-9]/", $address )) { //Invalid echo "The value '$address' is invalid"; //Add this for debugging } Psycho thank you very much for the reminder.. It's appreciated. But what I posted is just the summary of the code.. There's actually more into that like trimming, mysql escape and etc. Anyway, Thanks again and it will be for sure noted for future work Quote Link to comment https://forums.phpfreaks.com/topic/266860-how-to-check-if-a-variable-has-a-number-on-it/#findComment-1368174 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.