demeritrious Posted July 12, 2014 Share Posted July 12, 2014 Good evening, I am working on a birthday validation using Jquery datepicker. I am trying to make the birthday section validate whether the birthday input is in a date format and I want to prevent users under 18 from join. Thank you accountsettings.php <input type="text" name="Birthday" id="Birthday" maxlength="50" value=" <?php if($form->value("Birthday") == ""){ echo $session->userinfo['Birthday']; }else{ echo $form->value("Birthday"); } ?>"> process.php function register( $subuser, $subconfirmuser, $subpass, $subconfirmpass, $subemail, $subconfirmemail, $subFirstname, $subLastname, $subGender, $subBirthday, $subRegion, $subCountry, $subCity_State, $subQuestion, $subAnswer, $subConfirmAnswer){ global $database, $form, $mailer; //The database, form and mailer object /* Birthday error checking */ $field = "Birthday"; //Use field name for Birthday if(!$subBirthday || strlen($subBirthday = trim($subBirthday)) == 0){ $form->setError($field, "* Birthday not entered"); } else{ /* I've attempted different formulas but it did not work here */ $date = date('yyyy-mm-dd'); if( $subBirthday > $date){ $form->setError($field, "* You are too young!"); } } Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/ Share on other sites More sharing options...
fastsol Posted July 12, 2014 Share Posted July 12, 2014 This is not what you think it is for the date() value. $date = date('yyyy-mm-dd'); You want this instead $date = date('Y-m-d'); Your way would have produced a date string like this 1999199919991999-0101-3131 Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484822 Share on other sites More sharing options...
demeritrious Posted July 12, 2014 Author Share Posted July 12, 2014 (edited) So should I do a formula that involves? $date = date('-18Y-m-d'); Edited July 12, 2014 by demeritrious Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484824 Share on other sites More sharing options...
Solution fastsol Posted July 12, 2014 Solution Share Posted July 12, 2014 Assuming your date is being posted in a format like 2013-12-31 you could use this. if( $subBirthday > date("Y-m-d", strototime("-18 years", date()))){ $form->setError($field, "* You are too young!"); } 1 Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484826 Share on other sites More sharing options...
Miggy64 Posted July 12, 2014 Share Posted July 12, 2014 I find working with DateTime() easier: <?php $now = new DateTime(); $birthday = new DateTime('1964-08-28'); $years = $now->diff($birthday, true); echo $now->format('Y-m-d') . '<br>'; echo $years->y; // Number of years since birthday //var_dump($years); Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484827 Share on other sites More sharing options...
demeritrious Posted July 12, 2014 Author Share Posted July 12, 2014 I'm receiving this error: Fatal error: Call to undefined function strototime() Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484829 Share on other sites More sharing options...
demeritrious Posted July 12, 2014 Author Share Posted July 12, 2014 I could not figure out how to manipulate it to give me an error confirmation that says the data entered is not in the correct format or if the user is under 18 I find working with DateTime() easier: <?php $now = new DateTime(); $birthday = new DateTime('1964-08-28'); $years = $now->diff($birthday, true); echo $now->format('Y-m-d') . '<br>'; echo $years->y; // Number of years since birthday //var_dump($years); Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484830 Share on other sites More sharing options...
demeritrious Posted July 12, 2014 Author Share Posted July 12, 2014 By the way this worked for me for Invalid Email validation..maybe this might help someone: /* Email error checking */ $field = "email"; //Use field name for email if(!$subemail || strlen($subemail = trim($subemail)) == 0){ $form->setError($field, "* Please enter email address!"); } else{ /* Check if valid email address */ $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*" ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*" ."\.([a-z]{2,}){1}$"; if(!eregi($regex,$subemail)){ $form->setError($field, "* Email invalid"); } $subemail = stripslashes($subemail); } Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484831 Share on other sites More sharing options...
fastsol Posted July 12, 2014 Share Posted July 12, 2014 I'm receiving this error: Fatal error: Call to undefined function strototime() Sorry I misspelled the function, honestly you should know that It's strtotime() Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484832 Share on other sites More sharing options...
demeritrious Posted July 12, 2014 Author Share Posted July 12, 2014 AHHHHH I didn't even notice!!!...Thank you very much for your help! It works the way it is but is there a way to make sure it is in the Y-m-d format in case someone types in random data? This is what worked for me. /* Birthday error checking */ $field = "Birthday"; //Use field name for Birthday if(!$subBirthday || strlen($subBirthday = trim($subBirthday)) == 0){ $form->setError($field, " * Please enter birthday"); } else if( $subBirthday > date("Y-m-d", strtotime("-18 years"))){ $form->setError($field, "* You are too young!"); } Quote Link to comment https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484835 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.