Jump to content

Quick Help Needed About List Box Validation


kafia

Recommended Posts

hi every one in my form i want to use a list box for the user to specify his age. the code i write it like below.<?phpif (isset($_POST['submit'])){$m=$_POST['m']$day=$_POST['day'];$year=$_POST['year'];}if(empty($m)){$error=true;}else{header("location:somewhere.php");}?><html><body><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><select name="m"><?php if($error && empty($m)){echo 'Error messege ';}?><option value="" selected="selected"> ---select one---</option><option value="Jan"<?php if($m=="Jan" echo "selected";?>>Jan</option><option value="Feb"<?php if($m=="Feb" echo "selected";?>>Feb</option><option value="March"<?php if($m=="March" echo "selected";?>>March</option></select><input type="text" name="day" value="day" ><input type="text" name="year" value="year"><br><br><input type="submit" name="submit"></form></body></html>the scenario of my topic is that .if the user select Febroray as the month from the listbox. next he has to write the day of his birth and year .the day and the year are the input text box.so my question is that if the user choose febroray as the month then in the day box he could not write the figure more then the 29 as we know febroray is of 28 days and 29 days in leap year .same like if the user select a month that is of 30 days the user should be restrectid or limited to the range from 1 to 30 and if he write the 31 the form should show error msg and diei want it all with php only and at the same page where the form is . helping reply is welcomed thank you all in advance. kafia

Link to comment
Share on other sites

As php runs on the server andd not in the clients browser like js you would need to resubmit the data to the page for it to check and update the page content depending on what had been inputted.I would reccommend Javascript instead then once the script is submitted you can use php to check the data again before using the submitted content.

Link to comment
Share on other sites

First, PHP won't have a problem if the enter an invalid date. If they enter March 31, PHP will use April 1 (the day after March 30). That's assuming you use something like mktime to turn the date into a timestamp.If you want to show error messages instead, then you need to manually validate everything yourself. Keep a list of how many days go in each month, and for February you need to use the leap year algorithm to determine what the max number of days is. You can find that algorithm here:http://en.wikipedia.org/wiki/Leap_year

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...