2
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 validate1() {
    var oInput1 = document.getElementById("txtdate");
    if (isValidDate(oInput1.value)) {
        alert("Valid");
    } else {
        alert("Invalid!");
    }

}

I had written code for dateformat validations in javascript

and I called this function in textbox as onkeypress="return validate1(event);" but it is not firing. It is taking date as well as any data including date.

6
  • Better to use a datepicker and set date-format of your wish. Commented Nov 21, 2013 at 6:04
  • But the function is firing for me.. See the fiddle jsfiddle.net/WP37Q Commented Nov 21, 2013 at 6:04
  • You probably shouldn't use the keyword event in your onkeypress attribute definition, do you have any errors? Commented Nov 21, 2013 at 6:04
  • Check the Javascript console. Commented Nov 21, 2013 at 6:06
  • change the function definition from function validate1() to function validate1(e) and try again. Commented Nov 21, 2013 at 6:18

2 Answers 2

1
 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 validate1() {
       var oInput1 = document.getElementById("txtdate");
       if (isValidDate(oInput1.value)) {
           alert("Valid");
       } else {
           alert("Invalid!");
       }

   }
Sign up to request clarification or add additional context in comments.

Comments

0

Code is firing fine, suggestion use jquery datepicker...

<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 validate1() {
           var oInput1 = document.getElementById("txtdate");
           if (isValidDate(oInput1.value)) {
               alert("Valid");
           } else {
               alert("Invalid!");
           }

       }
</script>

    <input type="text" id="txtdate" name="txtdate" onkeypress="return validate1(event)">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.