locked
JQuery date validation for valid date and in MM/DD/YYYY format only RRS feed

  • Question

  • User1099429166 posted

    I have input type="text" and I am looking for some jquery validation. The validation should check for date in the format MM/DD/YYYY and it should also check if its a valid date. If its not a valid date or format then it should clear the input textbox. The validation should be checked on typing in the textbox and also on copy pasting in the input box. I can't use jQuery Plugin

    Thursday, December 27, 2018 4:43 PM

All replies

  • User475983607 posted

    jQuery Validation is a jQuery plugin.

    https://jqueryvalidation.org/

    I don't how you can use jQuery Validation without a plugin.  Are you asking the community to write custom JavaScript code?  If so, are you willing to share your fee?  What is wrong with using jQuery Validate?

    Thursday, December 27, 2018 5:01 PM
  • User283571144 posted

    Hi Sam Solomon,

    The validation should check for date in the format MM/DD/YYYY and it should also check if its a valid date. If its not a valid date or format then it should clear the input textbox. The validation should be checked on typing in the textbox and also on copy pasting in the input box. I can't use jQuery Plugin

    Do you mean you don't want to use jquery to achieve validating the date?

    If this is your requirement, I suggest you could try to use javascript regular expressions to determine the date format in the text box and use onchnage event of input type="text" to validate it. 

    More details, you could refer to below codes:

    <script>
           function isDate(txtDate) {
               var currVal = txtDate;
               if (currVal == '')
                   return false;
               var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; 
               var dtArray = currVal.match(rxDatePattern);
    
               if (dtArray == null)
                   return false;
               
               dtMonth = dtArray[1];
               dtDay = dtArray[3];
               dtYear = dtArray[5];
    
               if (dtMonth < 1 || dtMonth > 12)
                   return false;
               else if (dtDay < 1 || dtDay > 31)
                   return false;
               else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
                   return false;
               else if (dtMonth == 2) {
                   var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
                   if (dtDay > 29 || (dtDay == 29 && !isleap))
                       return false;
               }
               return true;
           }
    
           
      function vali()
           {
               var s = document.getElementById("getDate").value;
              
                   if (isDate(s) == false) {
                       document.getElementById("getDate").value = ' ';
                      
                   } else {
                       document.getElementById("getDate").value = s;
                       
                   }     
           }
       </script>
    
    <body>
        <div>
            enter a date(mm/dd/yyyy) <input type="text" id="getDate" onchange="vali()" />
          
        </div>
    </body>
    

    Result:

    Best Regards,

    Brando

    Friday, December 28, 2018 6:52 AM