Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date picker Validation

    Hi all , first let me just say I know incredibly little about JavaScript at all , I'm currently studying PHP however as part of a course and do occasionally use JavaScript here and there

    My problem is on a register page for example i have a JavaScript Date picker so users can select their D.O.B without me having to worry about if the date is valid ...etc through PHP checking.
    PHP Code:
    <tr>
                            <th>Date of Birth :</th>
                            <td input name="dob"  id="dob"><?php
              $thisyear
    getdate();
              
    $myCalendar = new tc_calendar("date5"truefalse);
              
    $myCalendar->setIcon("./images/iconCalendar.gif");
              
    $myCalendar->setDate($thisyear['year'],$thisyear['month'],$thisyear['day']);
              
    $myCalendar->setPath("./");
              
    $myCalendar->setYearInterval(1910, ($thisyear['year']));
              
    $myCalendar->dateAllow('1910-05-13', ($thisyear['year']-18) );
              
    $myCalendar->setDateFormat('j F Y');
              
    //$myCalendar->setHeight(350);
              //$myCalendar->autoSubmit(true, "form1");
              
    $myCalendar->writeScript();

              
    ?></td>
                      </tr>
    Now in the <head> tag of my page i do i my JavaScript validation , and was wondering what would i have to do for this to work
    Thus far i've tried .checked/.selected basically anything i can think of but since its not my forte there isn't a lot for me to think of
    PHP Code:
    <script type="text/javascript">
    function 
    validate(form) {
    if(!
    document.form1.agree.checked)
    {
    alert("Please check the terms and conditions");
    return 
    false;
    }
    if(!
    document.form1.dob.checked)
    {
    alert("Please select a date of birth");
    return 
    false;
    }

    return 
    true;
    }
    </script>
    </script> 
    Now i was wondering is there anyway to check if a valid date was selected ??
    if you can offer any help it'd be much appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-


    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    if (yr <1910 || yr >2009) {  // you may want to change 2009 to some other year!
    alert ("Impossible Year Of Birth!")
    return false;
    }
    
    mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
    var nd = new Date();
    nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD
    
    var ndmm = nd.getMonth();
    if (ndmm != mm) {
    alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
    return false; 
    }
    else {
    alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
    }
    }
    
    checkValidDate(2009,2,31)  // Example:- 31st February 2009
    </script>
    A woman only worries about the future until she gets a husband, while a man only worries about the future after he gets a wife.

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply but unfortunatly i think my lack of knowledge on JavaScript is going to cost me dear here.

    the example of the Date picker im using is found at http://www.triconsole.com/php/calendar_datepicker.php
    the version i use is the one marked <b>DatePicker with no input box</b>


    Unable to implement you function which by the way i am very grateful to you for giving , I had to do something which I didn't want to do .
    For the register you need to be over 18 , so what i previously had was an empty date field and clicking on the calender would bring up todays date and there would then be 18 years full of invalid dates to try and discourage picking the fist available date.
    But to make sure at least 1 valid date is pick I just added this line of code .
    PHP Code:
    $myCalendar->setDate(date('d'), date('m'), (date('Y')-18)); 

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    If the only use of the date picker is for the user to input their DOB, then I suggest you abandon it and replace with simple dropdowns for day, month, year (ending at 1992) and validate them with the code I gave you.

    As well as a valid DOB in the sense of a date which exists (not e.g. 31st February) you may wish to check the user's age which of course is not just simply the year of birth.

    Code:
    <script type="text/javascript"> 
    
    function checkAge(bmth,bdy,byr){ 
    
    var today = new Date(); 
    var nowyear = today.getFullYear();
    bmth = bmth - 1; // in JavaScript months are 0-11
    var age = nowyear - byr;
    var nowmonth = today.getMonth();  // months are 0-11
    var nowday = today.getDate();
    if (bmth > nowmonth) {age = age - 1}  // next birthday not yet reached
    else if (bmth == nowmonth && nowday < bdy) {age = age - 1}  // next birthday not yet reached
    alert('You are ' + age + ' years old'); 
    }
    
    checkAge(03,17,1968);  // MM/DD/YYYY format
    
    </script>
    But at the end of the day someone who is in truth too young to register will doubtless simply inflate his age.
    Last edited by Philip M; 04-19-2010 at 07:54 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •