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! Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/ 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. } ?> Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349818 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349820 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. Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349824 Share on other sites More sharing options...
Jessica Posted September 17, 2007 Share Posted September 17, 2007 That's what intval does. Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349827 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... Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349829 Share on other sites More sharing options...
darkfreaks Posted September 17, 2007 Share Posted September 17, 2007 Edit: nvm Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349835 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. Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349838 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. Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349852 Share on other sites More sharing options...
darkfreaks Posted September 17, 2007 Share Posted September 17, 2007 hit topic solved then Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349856 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. Link to comment https://forums.phpfreaks.com/topic/69617-validate-age/#findComment-349945 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.