worldcomingtoanend Posted March 13, 2011 Share Posted March 13, 2011 I need users to input ONLY numbers or letters with numbers. How can i go about fixing that. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/ Share on other sites More sharing options...
HuggieBear Posted March 13, 2011 Share Posted March 13, 2011 Give preg_match() a try. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187085 Share on other sites More sharing options...
sasa Posted March 13, 2011 Share Posted March 13, 2011 ctype_alnum() function Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187097 Share on other sites More sharing options...
mikecampbell Posted March 13, 2011 Share Posted March 13, 2011 ctype_alnum() will return true if the input is all letters, which the poster does not want. Try this: return preg_match('/^[0-9a-z]*[0-9][0-9a-z]*$/i', $test); Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187159 Share on other sites More sharing options...
Pikachu2000 Posted March 13, 2011 Share Posted March 13, 2011 ctype_alnum() will match alphanumeric characters, which is what the OP stated as the requirement. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187176 Share on other sites More sharing options...
mikecampbell Posted March 14, 2011 Share Posted March 14, 2011 As I read the requirements, he wants to match 1. numbers, or 2. numbers and letters ie, not just letters Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187186 Share on other sites More sharing options...
litebearer Posted March 14, 2011 Share Posted March 14, 2011 http://php.net/manual/en/function.ctype-alnum.php Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187188 Share on other sites More sharing options...
Pikachu2000 Posted March 14, 2011 Share Posted March 14, 2011 As I read the requirements, he wants to match 1. numbers, or 2. numbers and letters ie, not just letters Which is precisely what ctype_alnum() does, with 'alnum' representing AlphaNumeric. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187221 Share on other sites More sharing options...
sasa Posted March 14, 2011 Share Posted March 14, 2011 $bool = ctype_alnum($text) and !ctype_alpha($text); Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187230 Share on other sites More sharing options...
Pikachu2000 Posted March 14, 2011 Share Posted March 14, 2011 As I read the requirements, he wants to match 1. numbers, or 2. numbers and letters ie, not just letters You're right, you're right. Even after reading it 5 or 6 times, I still didn't catch that it couldn't be just letters. I swear I'm just dense sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/230535-how-can-i-validate-a-number-or-letters-with-numbers-with-php/#findComment-1187232 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.