pacchiee Posted September 17, 2007 Share Posted September 17, 2007 Hello, I wanted to validate user input of age to be a number and range between 0 to 99. How do I do this? Thanks in Advance! Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2007 Share Posted September 17, 2007 <?php $num = intval($age); if($num < 0 || $num > 99){ // not valid. } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 17, 2007 Share Posted September 17, 2007 Here is another way. <?php if (is_numeric($age) && $age >= 0 && $age < 100){ echo "You entered a valid age."; } else { echo "ERROR: Invalid Age"; } ?> Quote Link to comment Share on other sites More sharing options...
pacchiee Posted September 17, 2007 Author Share Posted September 17, 2007 Thats worked great! But I wanted it to be a whole number too.. please help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2007 Share Posted September 17, 2007 That's what intval does. Quote Link to comment Share on other sites More sharing options...
pacchiee Posted September 17, 2007 Author Share Posted September 17, 2007 Hi Jesi, But my code is still accepting and processing 22.5 years?? If user writes age as a decimal/fraction the error should be like "age should be a whole number". Me new to PHP.. so please... Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 17, 2007 Share Posted September 17, 2007 Edit: nvm Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 17, 2007 Share Posted September 17, 2007 darkfreaks, wtf? That will make 22.5 into 225. pacchiee, using intval() will make 22.5 into 22. If you want to check and see if they've entered 22.5 you could use strpos to determine if they've entered a . in the number. Quote Link to comment Share on other sites More sharing options...
pacchiee Posted September 17, 2007 Author Share Posted September 17, 2007 oh jesirose! thats out of my current knowledge of PHP . am now able to validate age though not decimals, thanks a lot. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 17, 2007 Share Posted September 17, 2007 hit topic solved then Quote Link to comment Share on other sites More sharing options...
phpmo Posted September 17, 2007 Share Posted September 17, 2007 Also you can always use floor($age); To drop of any decimals and anything after. 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.