how to validate user date input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragon52
    New Member
    • Jun 2009
    • 72

    how to validate user date input

    Hi all,

    I want to validate on the client side and I have the following php code (tbxBirthDate is the text box, ie name="tbxBirthD ate")
    [code=php]
    with(window.doc ument.enrollFor m)
    {
    .
    $strBirthDate = tbxBirthDate;
    $arr=split("/",$strBirthDate ); // splitting the array

    $dd=$arr[0];
    $mm=$arr[1];
    $yy=$arr[2];
    }
    if(!checkdate($ mm,$dd,$yy))
    {
    alert("Please enter your birthdate in dd/mm/yyyy fomat");
    cBirthDate.focu s();
    return false;
    }
    [/code]
    I keep getting the run time error "Error: Object expected" and I think it is the line $arr=split("/",$strBirthDate )

    Can anyone see what the problem is?

    Is this the best way to validate a date?

    thanks
    Denis
    Last edited by Atli; Jun 8 '09, 10:01 PM. Reason: Added [code] tags.
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    hey..
    have a look on this example , would help you

    Javascript Date Validation - mm/dd/yyyy format


    :)

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Are you mixing JavaScript with PHP?
      If you are, then that is never going to work.

      PHP is executed server side, before the browser even knows that it is getting Javascript code to execute.

      The two can not be mixed together. (Although AJAX can call PHP scripts if needed.)

      Comment

      • prabirchoudhury
        New Member
        • May 2009
        • 162

        #4
        It a date validation and that could be on server side (php get more server load) or on the client side (JavaScript) or could use both (Ajax). He is doing on client side validation using JavaScript. JavaScript and php could work together if the form get posted then

        Code:
        <script type="text/JavaScript">
        <?php
              if (!empty($_POST[tbxBirthDate])){
        ?>
         alert('<?php echo $_POST[tbxBirthDate]; ?>');
        <?
         }
         ?>
        
        </script>

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by prabirchoudhury
          JavaScript and php could work together if the form get posted then
          Not really, no.

          PHP can output Javascript for the client to execute (just as it outputs HTML), but the two can't directly work together. Or more specifically, Javascript can't work with PHP. (Except for using AJAX, of course... and even that barely qualifies.)

          You can obviously have Javascript submit values via a forum submission, but that is rarely an acceptable method for client-side code.

          Comment

          • prabirchoudhury
            New Member
            • May 2009
            • 162

            #6
            how to validate user date input

            Javascript and PHP they both execute in different place and different way but they could work togather is complately different how they execute. they could share the result and output and that would be in client side..

            In Ajax also they execute in different place but from the client to server side xml work with them and make xmlHttp request..

            :)

            Comment

            • dragon52
              New Member
              • Jun 2009
              • 72

              #7
              Thanks all for your inputs.

              Sorry I haven't responded earlier as I have been using/testing you guy's suggestions.

              Your comments made me think and looked at my code again. I was in fact confused between script and php code.

              I am now using
              [code=javascript]
              var strDate = tbxBirthDate.va lue;
              var arr = strDate.split("/"); // splitting the array[/code]

              and I found the following function
              [code=javascript]
              <script language="JavaS cript">
              function isValidDate(dd, mm,yyyy)
              {
              var strDate = mm + "/" + dd + "/" + yyyy;
              var dt = new Date(strDate);

              if (dt.getDate() != dd)
              {
              return false;
              }
              if (dt.getMonth() != mm - 1)
              {
              // Javascript starts month from 0
              return false;
              }
              if (dt.getFullYear () != yyyy)
              {
              return false;
              }
              return true;
              }
              </script>[/code]

              This seems to work. The example in prabirchouhury' s link is very complicated. I can't understand the need to eg check year range etc.

              thanks to you all
              Denis
              Last edited by Atli; Jun 9 '09, 02:12 PM. Reason: Added [code] tags.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by dragon52
                I can't understand the need to eg check year range etc.
                on some systems the year is saved using 2 digits (originating from times, where memory was a valuable good) so if Date.getYear() returns 04, it could mean 1904 or 2004 now (that was no problem until 2000…).

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  The Y2K Problem. Shortsightednes s at it's best.

                  Comment

                  • dragon52
                    New Member
                    • Jun 2009
                    • 72

                    #10
                    I'll check total length

                    Yes, good point.

                    I will check for a fixed length of 10. That should stop people entering values such as 12/3/1988 or 10/10/04.

                    thanks

                    Comment

                    Working...