Php code for D.O.B drop down selection and validation


Date of Birth (DOB) is asked in every registration form now a day. It is also used as password in more of the examination.

html code
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</body>
<body bgcolor="thistle">
<fieldset>
<legend>Date of Birth and Validation</legend>
<form method="post" action="">
Enter your D.O.B :-  <select name="date">
<option value="date">date</option>
<?php for($i=1$i<=31$i++)
{ ?>
<option value="<?php echo $i; ?>"><?php echo $i?></option>
<?php } ?>
</select>

<select name="month">
<option value="month">month</option>
<?php for($i=1; $i<=12; $i++)
{ ?>
<option value="<?php echo $i?>"><?php echo $i?> </option>
<?php ?>
</select>

<select name="year">
<option value="year">year</option>
<?php for($i=1980; $i<=2012; $i++)
?>
<option value="<?php echo $i?>"><?php echo $i?></option>
<?php } ?>
</select>
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($msg))
{
echo "<script>alert('$msg')</script>";
echo "<font color=purple size=6px>" . $msg . "</font>";
}
?>
</fieldset>
</body>
</html>

php code
<?php
if(isset($_POST['submit']))
{
$date = $_POST['date'];
$month = $_POST['month'];
$year = $_POST['year'];

if($year % 4 > 0)
{
if($month == 2)
{
if($date > 28)
{
$error = "please provide correct dob";
}
}
}

if($year % 4 == 0)
{
if($month == 2)
{
if($date > 29)
{
$error = "please provide correct dob";
}
}
}

if($month == 4 || 6 || 9 || 11)
{
if($date > 30)
{
$error = "please provide correct dob";
}
}

if($date === 'date')
{
$msg = "please select Date";
}
else if($month === 'month')
{
$msg = "please select Month";
}
else if($year === 'year')
{
$msg = "please select Year";
}
else if(isset($error))
{
echo "<script>alert('$error')</script>";
}
else
{
$msg = "Your Date of Birth is" . " " . $date . "/" . $month . "/" . $year;
}
}
?>

No comments: