0

how do validate and to show its age through date of birth?

if ($_POST['DateOfBirth_Month'] == 'January') {
     $DateOfBirth_Month = $_POST['DateOfBirth_Month'];// January
} 
else if ($_POST['DateOfBirth_Month'] == 'February') {
     $DateOfBirth_Month = $_POST['DateOfBirth_Month'];// February
}

if ($_POST['DateOfBirth_Day'] == '1') {
    $DateOfBirth_Day = $_POST['DateOfBirth_Day'];// 1
} 
else if ($_POST['DateOfBirth_Day'] == '2') {
    $DateOfBirth_Day = $_POST['DateOfBirth_Day'];// 2
}



if(is_numeric($_POST['DateOfBirth_Year']) && $_POST['DateOfBirth_Year'] > 1969)
{     
    $DateOfBirth_Year = $_POST['DateOfBirth_Year'];
 } 
else if (is_numeric($_POST['DateOfBirth_Year']) && $_POST['DateOfBirth_Year'] < 1970)
{
     echo'<p><font color ="red">Please enter 1970 and above!</font></p>'; 
} 
else 
{
     echo '<p><font color="red">Enter NUMBER for the year of date of birth!</font></p>'; 
} 
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • my year is textbox input – user3151494 Jan 05 '14 at 15:21
  • if(is_numeric($_POST['DateOfBirth_Year']) && $_POST['DateOfBirth_Year'] > 1969){ $DateOfBirth_Year = $_POST['DateOfBirth_Year']; } else if (is_numeric($_POST['DateOfBirth_Year']) && $_POST['DateOfBirth_Year'] < 1970){ echo'

    Please enter 1970 and above!

    '; } else { echo '

    Enter NUMBER for the year of date of birth!

    '; }
    – user3151494 Jan 05 '14 at 15:21
  • Can you clarify what your question is? (And edit any new code into the question directly) – Pekka Jan 05 '14 at 15:21
  • I want to set validation for month and day from drop down list that certain month have certain day like february id 28 days....i also want to show age through date of birth – user3151494 Jan 05 '14 at 15:26
  • So what are you stuck with? What in your above code doesn't work? – Pekka Jan 05 '14 at 15:27
  • The question name and the main body text do not match at all. If you're talking about finding the age of someone by using a date of birth, [this](http://stackoverflow.com/questions/10186811/calculating-age-from-date-of-birth-in-php) will help you. Otherwise, please be more clear on what you need. – GiamPy Jan 05 '14 at 15:29
  • i do not know how do i validate that certain month have certain number of day....i also dont how to show age through date of birth – user3151494 Jan 05 '14 at 15:29
  • @user3151494 - Just look at a calendar? – Ed Heal Jan 05 '14 at 15:47

2 Answers2

0

Personally I'd prefer validation at client side using JS. You may try using the regex

/^\d{2}\/\d{2}\/\d{4}$/

to validate date. You may get few ideas from this fiddle

  • You always have to do validation at server side. Client side just makes a better users experience – Ed Heal Jan 05 '14 at 15:32
  • Just curious, but validation at client side should ensure a safe entry to server side right? I mean, a valid client side entry cannot be suddenly an invalid server side entry; especially while using ajax. Can it? –  Jan 05 '14 at 15:36
  • Javascript disabled? People injecting faulty data if they desire to mess up your system. Basically do not trust data from the client – Ed Heal Jan 05 '14 at 15:38
0

Let's assume I post my birthday:

$_POST["DateOfBirth_Year"] = 1980;
$_POST["DateOfBirth_Month"] = "October";
$_POST["DateOfBirth_Day"] = 20;

I use this function for handling POST

function POST($var) {
  if(!isset($_POST[$var])) return null;
  return $_POST[$var];
}

First we read the input

$dob_m = POST('DateOfBirth_Month');
$dob_y = intval(POST('DateOfBirth_Year'));
// lets allow say 24th to be 24
$dob_d = intval(POST('DateOfBirth_Day'));

For easy checking and getting the month index, let's create months array

$months = array("January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December");

Now check the month

$month_ok = array_search($dob_m,$months)!==false;
if(!$month_ok) return; // some reasonable error handling should be here

...and get month index

$dob_m = array_search($dob_m,$months) + 1;

Then we should validate the date (30.2.1980 would be invalid) using date function

$stamp = mktime(0,0,0,$dob_m,$dob_d,$dob_y);
$dob_ok = date("Y",$stamp)==$dob_y &&
    date("n",$stamp)==$dob_m && date("j",$stamp)==$dob_d;
if(!$dob_ok) return; // proper error handling here, too

Last, let's compute the age using DateTime object

$dob_dt = new DateTime(date("Y-m-d H:i:s",$stamp));
$now_dt = new DateTime("now");
$age = $dob_dt->diff($now_dt)->format("%y");

if($age<0 || $age>125) return; // error handling here
echo $age; // should be my age
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169