How do I Verify Date Format - mm/dd/yyyy - using Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerrydigital
    New Member
    • Oct 2008
    • 67

    #1

    How do I Verify Date Format - mm/dd/yyyy - using Javascript

    Hi,

    I have a registration form that uses the javascript validation code I have posted below. I am trying to verify that the user enters a birthdate in the format 01/11/1999

    Anyone know how I can incorporate a date format code into my existing code?

    Thanks for any help - Jerry

    Code:
    <script type="text/javascript">
    <!--
    function validate_form ( )
    {
    valid = true;
    if ( document.registration.firstname.value == "")
    {
    alert ( "Please fill in the 'First Name' box." );
    document.registration.firstname.focus();
    return false;
    }
    if ( document.registration.birthdate.value == "")
    {
    alert ( "Please fill in the 'Birthdate' box." );
    document.registration.birthdate.focus();
    return false;
    }
    return valid;
    }
    //-->
    </script>
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hello Sir,

    Are u using any calendar to get the date value from the user on the user must enter the date in the input in the date format as u have specified.


    Regards
    Ramanan Kalirajan

    Comment

    • jerrydigital
      New Member
      • Oct 2008
      • 67

      #3
      Hi,

      I am asking the user to enter their birthdate and would prefer if they would enter the date the way I wish.

      For instance, I have a form and it says:
      Birthdate(pleas e use format mm/dd/yyyy): "text box"

      I would like a box to pop up just like my other javascript that says "Please enter a date in the following format "mm/dd/yyyy" if the user fails to do so.

      Is there a code I can implement into the code I already have(posted above)?

      thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Probably the best method would be to use a regular expression to pattern match the input. Off the top of my head, something like
        Code:
        /^\d{2}\/\d{2}\/\d{4}$/
        Of course, this only matches the format, but doesn't validate that the date is an actual valid one.

        Comment

        • jerrydigital
          New Member
          • Oct 2008
          • 67

          #5
          Thank you for your post.

          I am not sure how to implement this code you speak of into my script. Any ideas?

          Thanks

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You can use the RegExp object or use the string methods for matching.

            Some links:
            Detailed description of the capabilities of the JavaScript RegExp Object, defined in the ECMA-262 standard.

            The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.


            Hope those help. If you get stuck, post your code.

            Comment

            Working...