squaringkarma Posted February 12, 2009 Share Posted February 12, 2009 How would you validate a form to make sure a user entered in a number for a specific field? I'm blanking on that right now. I'm working on a site where there are 3 different forms depending on what a user inputs for their age. If the user does not enter their age, I want the initial form to reappear with an error of "Please enter your age." Right now, the initial form only appears when the trimmed string length for the field is 0. Quote Link to comment Share on other sites More sharing options...
marcus Posted February 12, 2009 Share Posted February 12, 2009 Now that I think of it, if you're checking for age you might want to look at http://php.net/ctype_digit or http://php.net/is_int is_numeric will return true for decimal values. Quote Link to comment Share on other sites More sharing options...
metrostars Posted February 12, 2009 Share Posted February 12, 2009 if(!is_numeric($value)) { //Value is not numeric } else { // Value is numeric } Quote Link to comment Share on other sites More sharing options...
squaringkarma Posted February 12, 2009 Author Share Posted February 12, 2009 Hm thanks. mgallforever, when I try with is_int, I always get false. I presume this is because it's form input and it's reading it as a string. I guess my next question would be how do you convert a string into a number? (Though the ctype_digit is accepting strings as input, so maybe the answer to my question isn't necessary.) Quote Link to comment Share on other sites More sharing options...
FD_F Posted February 12, 2009 Share Posted February 12, 2009 try use something like this : $num = "1"; $bol = ctype_digit($num); if ($bol == True) { echo "true"; } else { echo "false"; } Quote Link to comment 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.