dfowler Posted January 30, 2008 Share Posted January 30, 2008 Hey guys, I have a form that I need help validating. The only options the form field can have are either numbers or empty. I've tried is_numeric($value); but that doesn't help if the field is left empty. I also tried $number_format = "([0-9]{0,2})"; but had the same results. I would greatly appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/ Share on other sites More sharing options...
The Little Guy Posted January 30, 2008 Share Posted January 30, 2008 <?php $value = trim($value); if(is_numeric($value)||empty($value)){ //field is good }else{ //field is bad } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/#findComment-453644 Share on other sites More sharing options...
dfowler Posted January 30, 2008 Author Share Posted January 30, 2008 That still doesn't seem to work. Do you think I could set the $number_format to letters and characters and then through the error if the field is like the format? Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/#findComment-453664 Share on other sites More sharing options...
The Little Guy Posted January 30, 2008 Share Posted January 30, 2008 Maybe something like this: <?php $value = trim($value); if(is_numeric($value)){ $good = TRUE; }elseif(empty($value)){ $good = TRUE; }else{ $good = FALSE; } if($good){ //Field was good }else{ //Field was bad } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/#findComment-453669 Share on other sites More sharing options...
revraz Posted January 30, 2008 Share Posted January 30, 2008 ctype works good too. Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/#findComment-453679 Share on other sites More sharing options...
dfowler Posted January 30, 2008 Author Share Posted January 30, 2008 Thanks Little Guy, that worked. I changed the error to the following, thanks for you help. I don't know why it was being so tricky. if(!$good) { echo "<tr> <td colspan='4'><h1>Your input is invalid, please go back and correct this.</h1></td> </tr> <tr> <td><a href='javascript:history.go(-1)' title='fix'>Go back</a></td> </tr> </table>"; include 'footer.php'; exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/88603-solved-number-validation/#findComment-453698 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.