javascript - Check date on text focus out not working -
i want when user has entered date , text focus out cursor moved somewhere else, if date not in dd/mm/yyyy format , date out of range, error message should displayed...
function isvalid(str) { var ispattern = /^ (0?[1 - 9] |[12][0 - 9] | 3[01])[\/\/](0?[1 - 9] | 1[012])[\/\/]\d{ 4}$/.test(str); if (!ispattern) return false; var d = new date(str); return (!isnan(d)); } $(document).ready(function() { $("#newstartdate").bind("onfocusout", function(e) { var val = this.value; if (!isvalid(val)) alert("hello"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="newstartdate" data-mask="99/99/9999" type="text" style="width: 100px;" placeholder="start date(dd/mm/yyy)" required> please !!!
try use on blur instead of onfocusout
example below.
function isvalid(str) { var ispattern = /^(0?[1-9]|[12][0-9]|3[01])[\/\/](0?[1-9]|1[012])[\/\/]\d{4}$/.test(str); if (!ispattern) return false; var d = new date(str); return (!isnan(d)); } $(document).ready(function() { $("#newstartdate").bind("blur", function(e) { var val = this.value; console.log(!isvalid(val)) if (!isvalid(val)) alert("hello"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="newstartdate" data-mask="99/99/9999" type="text" style="width: 100px;" placeholder="start date(dd/mm/yyy)" required>
Comments
Post a Comment