Jump to content

Validating a Date


Phear46

Recommended Posts

OK, so i want my form adds a persons details to a DB, all good so far but i want to grab a birth-date with the form then input that to my DB. mySQL only accepts yyyy-mm-dd format so i need to convert my string to that format no-matter what.

 

But what if the person doesn't enter the date correctly? what if the date doesn't even exist?

 

I could 'guide' the user into inputting into the form in the correct format but Ive never seen this done on line anywhere. Everywhere ive ever enter a date has always accepted:

 

dd-mm-yy

dd-mm-yyyy

mm-dd-yy

mm-dd-yyyy

 

I can probably figure out formatting to mySQL format by myself but im totally confused by what i should do when the user enters a date like:

01-03-88

is this 1st March 1988 Or 3rd January 1988?

 

Is there a setting i can grab from the browser to determine where in the world a user is?

 

Link to comment
Share on other sites

don't give the user a choice on how to enter the date. use three specific select/option menus for the year, month, and day (add a date picker pop-up if desired to set the three select/option values.) then all you need to do is make sure the entered date is an actual date.

Link to comment
Share on other sites

;D Might this snippet will help you :

<?php
$date = '2003-04-31';
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)){
    echo 'Valid';
}else{
    echo 'Not Valid';
}
?>

 

If you do it that way (with a fixed format, contrary to the requirements) do a preg_match() for the basic format then a checkdate for the real validation. Writing a regex for all valid dates is pretty complex.
Link to comment
Share on other sites

All good suggestions guys, gone with drop downs for now as it seems the most basic way. Here is the code im using to draw the menu:

	<select name="day">
		<?php for ($d=01; $d<=31; $d++) {echo "<option value='$d'>$d</option>";}?>
	</select>
	<select name="month">
		<?php for ($m=01; $m<=12; $m++) {echo "<option value='$m'>$m</option>";}?>
	</select>
	<select name="year">
		<?php for ($y=date("Y"); $y>=1970; $y--) {echo "<option value='$y'>$y</option>";}?>
	</select>	

The only issue im having is that the values are coming up as '1, 2, 3, 4' etc etc instead of '01, 02, 03, 04' Is there a way i can force the counter to be at least two digits? I could check values when i process the form but it seems more logical to get this sorted with the actual form values that are submitted.

Edited by Phear46
Link to comment
Share on other sites

This might help you :happy-04: :

<select name="day">
		<?php for ($d=01; $d<=31; $d++) { $d = sprintf("%02d", $d); echo "<option value='$d'>$d</option>";}?>
	</select>
	<select name="month">
		<?php for ($m=01; $m<=12; $m++) {$m = sprintf("%02d", $m); echo "<option value='$m'>$m</option>";}?>
	</select>
	<select name="year">
		<?php for ($y=date("Y"); $y>=1970; $y--) {echo "<option value='$y'>$y</option>";}?>
	</select>
Link to comment
Share on other sites

I could check values when i process the form but ...

You absolutely must check the values when the form is submitted. It is a trivial matter for a user (i.e. bad guy) to send any data they want regardless of the values (OPTIONS) in your SELECT list.

Link to comment
Share on other sites

I could 'guide' the user into inputting into the form in the correct format but Ive never seen this done on line anywhere. Everywhere ive ever enter a date has always accepted:

Requesting a particular format is generally what I do. Now days I use the new type="date" input + some JS to make it more compatible. In the past I've done separate select boxes or just a text note describing the required format.

 

I've seen such things fairly often out on the web, even some sites that are so strict they reject a date if you don't add leading zeros (ie, 05/01/13 would be ok, but 5/1/13 would be invalid).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.