Knb15 Posted April 16, 2010 Share Posted April 16, 2010 Hi, I'm new to PHP, and have been searching forums to get some help with form validating and sanitizing. I got a simple form validating code from a tutorial, and tried it just to get started. I would like to know if i am doing anything wrong, or if perhaps there's a syntax error in the code that i could not find, because a condition in the code just does not work: HTML Form: <body> <form action="example04.php" method="post" > Enter your age: <input name="age" size="2"> <input type="submit" name="submit" value="Go"> </form> </body> 1) PHP Code: <?php if (!filter_has_var(INPUT_POST, 'submit')) { echo "form"; // include the form. } $age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT); if (is_null($age)) { echo "The 'age' field is required.<br />"; } elseif ($age === FALSE) { echo "Please enter a valid age.<br />"; } else { echo "Welcome.<br/>"; } ?> 2) I have also tried the following: <?php if(!empty($age)) //check if the age field is empty { if(is_numeric($age)) //check if user has given alphabets as age { echo 'Welcome.'; } else { echo 'Please enter a valid age.'; } } else { echo 'The age field is required.'; } ?> Script #1, when the input is null, or nothing is written, it is not returning "The age field is required." Instead it is returning "Please enter a valid age." However, if i enter a valid age or if i enter an invalid character, the proper response is returned. Script #2, solved the problem of the empty or null input field, however, it will not recognize when a character other than a number is type...the same response is returned for an empty input and for an input other than a number. Probably something very minor that i'm not familiar with yet, but i appreciate it anyway! Thanks Knb15 Link to comment https://forums.phpfreaks.com/topic/198783-trouble-validating-form-input/ Share on other sites More sharing options...
ChemicalBliss Posted April 16, 2010 Share Posted April 16, 2010 is_numeric(); Think about what this function does. ctype_alnum(); I think this is what your looking for. -cb- Link to comment https://forums.phpfreaks.com/topic/198783-trouble-validating-form-input/#findComment-1043357 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.