Jump to content

Birthday validation


demeritrious

Recommended Posts

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!");
         } 
}

 
Link to comment
https://forums.phpfreaks.com/topic/289800-birthday-validation/
Share on other sites

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);
Link to comment
https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484830
Share on other sites

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);
      }
Link to comment
https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484831
Share on other sites

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!");
}      

Link to comment
https://forums.phpfreaks.com/topic/289800-birthday-validation/#findComment-1484835
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.