macattack Posted May 22, 2008 Share Posted May 22, 2008 I have written a messy and likely very inefficient code to validate a form in PHP. Unfortunately, no matter what I put in for values, it returns that the year is invalid, and stops the process. Also, can anyone suggest how to check to make sure the date isn't in the future? Thanks in advance! The code begins with elseif because it's the second form to be validated. Form one works. elseif($_GET['addold'] == 'Add Old'){ if(!strlen($_POST['year'] == '4')){ echo "Year is invalid"; echo $old; } elseif(!is_numeric($_POST['year'])){ echo "Year must be numeric"; echo $old; } elseif(!strlen($_POST['month'] == '2')){ echo "Month is invalid"; echo $old; } elseif($_POST['month'] > '12'){ echo "Month is invalid"; echo $old; } elseif(!is_numeric($_POST['month'])){ echo "Month must be numeric"; echo $old; } elseif(!strlen($_POST['day'] == '2')){ echo "Day is invalid"; echo $old; } elseif(!$_POST['day'] < '31'){ echo "Day is invalid"; echo $old; } elseif(!is_numeric($_POST['day'])){ echo "Day must be numeric"; echo $old; } elseif(!strlen($_POST['title'] == "")){ echo "Title is invalid"; echo $old; } else{ displayAddOldBlogForm(); } } and here is the form in question $old = '<form action="index.php?addold=Add Old" method="POST"> <label for="title">Title:</label><input type="text" name="title"><br> <label for="year">Year (YYYY):</label><input type="text" name="year" maxlength="4"><br> <label for="month">Month (MM):</label><input type="text" name="month" maxlength="2"><br> <label for="day">Day (DD):</label><input type="text" name="day" maxlength="2"><br> <input type="submit" value="Write post for the specified date with this name"><br> </form>'; Quote Link to comment https://forums.phpfreaks.com/topic/106755-solved-form-validation-issue/ Share on other sites More sharing options...
mlin Posted May 22, 2008 Share Posted May 22, 2008 this line has an error: if(!strlen($_POST['year'] == '4')){ should be: if(!strlen($_POST['year']) == '4'){ see it? Quote Link to comment https://forums.phpfreaks.com/topic/106755-solved-form-validation-issue/#findComment-547249 Share on other sites More sharing options...
rameshfaj Posted May 22, 2008 Share Posted May 22, 2008 What will happen if the user enters 0000-00-00 for the design of your date pattern? You need to check if the date is some valid one. To get that the entered date is not that of the future one, get the system date using functions like now() and then get the posted year and compare these two values. You can manipulate in any ways as you like. It will be better for the months and the days to have some selection fields rather than the text field and then checking for the length. For the year also it can be the selection field but it may have some large entries so text field is also good but be sure that the user enters the date pattern as you desire. Quote Link to comment https://forums.phpfreaks.com/topic/106755-solved-form-validation-issue/#findComment-547279 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.