oliverj777 Posted March 22, 2012 Share Posted March 22, 2012 Hello, I'm working on an error form, and I want to check if the user inserted a number into the name field, and if so return an error. I thought this might of worked, but it doesn't: if(is_int($_POST['name'])){ $arrErrors['name'] = 'Invalid name, remove numbers.'; } Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/259468-check-if-string-contains-a-number/ Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 This can do that <?php $text=$_POST['name']; if (ereg('[^0-9]', $text)) { $arrErrors['name'] = "Invalid name, remove numbers."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259468-check-if-string-contains-a-number/#findComment-1330085 Share on other sites More sharing options...
QuickOldCar Posted March 22, 2012 Share Posted March 22, 2012 ereg was deprecated as of php version 5.3, might be better off using preg_match instead Quote Link to comment https://forums.phpfreaks.com/topic/259468-check-if-string-contains-a-number/#findComment-1330093 Share on other sites More sharing options...
dragon_sa Posted March 22, 2012 Share Posted March 22, 2012 I hadn't checked that thanks, here is a preg_match version <?php $text=$_POST['name']; if (preg_match('#[\d]#', $text)) { $arrErrors['name'] = "Invalid name, remove numbers."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259468-check-if-string-contains-a-number/#findComment-1330097 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.