Phear46 Posted May 15, 2013 Share Posted May 15, 2013 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 https://forums.phpfreaks.com/topic/278019-validating-a-date/ Share on other sites More sharing options...
mac_gyver Posted May 15, 2013 Share Posted May 15, 2013 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 https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430177 Share on other sites More sharing options...
Phear46 Posted May 15, 2013 Author Share Posted May 15, 2013 been thinking that since i posted.... probably a much better/easier way of doing it! Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430178 Share on other sites More sharing options...
CrossMotion Posted May 15, 2013 Share Posted May 15, 2013 the strtotime() function mostly gets it right. but I agree with mac_gyver. use a date picker or at least drop-down menus for day, month, year to be 100% safe. Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430190 Share on other sites More sharing options...
buzzycoder Posted May 15, 2013 Share Posted May 15, 2013 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'; } ?> Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430272 Share on other sites More sharing options...
requinix Posted May 15, 2013 Share Posted May 15, 2013 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 https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430282 Share on other sites More sharing options...
buzzycoder Posted May 15, 2013 Share Posted May 15, 2013 Yeah,that would be helpful too.Thanks requinix,for pointing me out. Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430290 Share on other sites More sharing options...
Phear46 Posted May 17, 2013 Author Share Posted May 17, 2013 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. Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1430695 Share on other sites More sharing options...
buzzycoder Posted May 23, 2013 Share Posted May 23, 2013 This might help you : <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 https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1431927 Share on other sites More sharing options...
DavidAM Posted May 24, 2013 Share Posted May 24, 2013 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 https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1432028 Share on other sites More sharing options...
kicken Posted May 24, 2013 Share Posted May 24, 2013 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 https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1432031 Share on other sites More sharing options...
Barand Posted May 24, 2013 Share Posted May 24, 2013 strict or just plain sloppy? Link to comment https://forums.phpfreaks.com/topic/278019-validating-a-date/#findComment-1432054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.