Javascript date validation using dd/mm/yy format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Krazy K
    New Member
    • Dec 2006
    • 3

    Javascript date validation using dd/mm/yy format

    Can anyone please guide me to a good resource for date validation in Javascript using dd/mm/yy format?
    There are many sites using other formats but I have not come across
    anything that uses dd/mm/yy.

    Thanks in advance
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You must be from the UK (just a guess!).

    Split the string using date.split()

    This should give you the day, month and year in an array assuming that it has been entered in the correct format in the first place. You can use a regular expression to check for that. Then adapt the code that you have found which already checks for the validity of the date, month and year, but in a different order.

    If you need more help, post again.

    Comment

    • Krazy K
      New Member
      • Dec 2006
      • 3

      #3
      Originally posted by acoder
      You must be from the UK (just a guess!).

      Split the string using date.split()

      This should give you the day, month and year in an array assuming that it has been entered in the correct format in the first place. You can use a regular expression to check for that. Then adapt the code that you have found which already checks for the validity of the date, month and year, but in a different order.

      If you need more help, post again.
      Thank you for responding but I am sorry I did not quite understand the date.split(). I was looking for a resource that has the code already written which does date validation in dd/mm/yy format as opposed to dd/mm/yyyy.

      I am just learning javascript and I need to get some date form fields validated. I was hoping to 'borrow' code that has already been written to do this, just don't know where to look for it since a search on google gives too many results.
      Ta

      Comment

      • AricC
        Recognized Expert Top Contributor
        • Oct 2006
        • 1885

        #4
        Originally posted by Krazy K
        Thank you for responding but I am sorry I did not quite understand the date.split(). I was looking for a resource that has the code already written which does date validation in dd/mm/yy format as opposed to dd/mm/yyyy.

        I am just learning javascript and I need to get some date form fields validated. I was hoping to 'borrow' code that has already been written to do this, just don't know where to look for it since a search on google gives too many results.
        Ta
        There is some Date info here. Here is an article on validating dates.

        Comment

        • Krazy K
          New Member
          • Dec 2006
          • 3

          #5
          Thanks for your help. Much appreciated. KK

          Comment

          • razmataz
            New Member
            • Jun 2009
            • 2

            #6
            Hi,

            Try using this :)

            Code:
            <script type="text/javascript">
                function isValidDate(sText) {
                    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
                    return reDate.test(sText);
                }
                function validate() {
                    var oInput1 = document.getElementById("txt1");
                    if (isValidDate(oInput1.value)) {
                        alert("Valid");
                    } else {
                        alert("Invalid!");
                    }
            
                }
            </script>
            Last edited by Dormilich; Jun 1 '09, 11:01 PM. Reason: Please use [code] tags when posting code

            Comment

            • deepak patel
              New Member
              • Sep 2011
              • 1

              #7
              this will not work for 29th Feb 2011. :(

              Comment

              Working...