Jump to content

Birthday validation


demeritrious
Go to solution Solved by fastsol,

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
Share on other sites

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