Date validation not working properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shepard
    New Member
    • Feb 2013
    • 24

    Date validation not working properly

    I want a date field to be validated and this is my code:
    It has From date & To date:

    Code:
    <script>function checkdate(frmdt,todt){
        var validformat=/^\d{2}\-\d{2}\-\d{4}$/
        var returnval=false
           if(!validformat.test(frmdt.value)){
               alert("Invalid frmdt");
               document.form.frmdt.value="";
       }
           else if(!validformat.test(todt.value)){
           alert("Invalid Date 2");
            document.form.todt.value="";
       }
            else{
            var start = document.form.frmdt.value;
             var end = document.form.todt.value;
    
             var stDate = new Date(start);
            var enDate = new Date(end);
           var compDate = enDate - stDate;
    
            if(compDate >= 0)
           return true;
             else
                {
              alert("End date should be greater than start date.");
              return false;
             }
               }
             }</script>
    But this code accepts every characters on the keyboard and gives a popup "error" accepting the characters...

    It also gives error even when proper date is entered..

    For eg if I enter 1, it doesnot wait for me to enter next no. and displays error while accepting 1. It does this with every character.

    I cant figure out what is wrong in the code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We would need to see how the code is called.

    Comment

    • Shepard
      New Member
      • Feb 2013
      • 24

      #3
      This is how i defined my field
      Code:
      <input id="date" type="text" onkeydown="return checkdate(frmdt,todt)" maxlength="10">

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That's why it's running on every key press. Because you used the onkeydown event. That fires every time a key is pressed. So it's going to check the first time you hit a key. You're probably looking for the onblur event or something similar. onblur only fires when the user leaves the control.

        There are also probably other problems but that's the first one we need to take care of before we know if there are others.

        Comment

        • Shepard
          New Member
          • Feb 2013
          • 24

          #5
          i tried onblur,onkeyup, onchange,etc.. only onkeypress and onkeyup atleast give an error for every character.

          Rest don't give any error and accepts everything.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That's because there's more errors in the code. But you first need to pick the right event. Most likely either onblur or onchange.

            After fixing that error, the next one you will need to address is how you reference the html elements. I don't believe you can access it that way, you will most likely need to use the getElementById method.

            After that error, we will need to see what other errors come up.

            Comment

            Working...