javascript - How to get value from text input on blur of field -
this question has answer here:
i'm trying write javascript value 2 text inputs, should straightforward. amiss. here code:
<script> jquery(function() { jquery('#frompicker, #topicker').datepicker({ dateformat : 'yy-mm-dd' }); jquery('#submit').attr('disabled', 'disabled'); jquery('#topicker').on('blur', function() { var fromdate = jquery('#frompicker').val(); var todate = jquery('#topicker').val(); console.log(todate); if (fromdate !== '' && todate !== '') { if (isvaliddate(fromdate) && isvaliddate(todate)) { jquery('#submit').removeattr('disabled'); } else { alert('you must enter dates in format "yyyy-mm-dd"'); } } }); }); function isvaliddate(dt) { if (dt.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/)) { return true; } } </script>
when console.log(todate)
however, empty string. if blur
event again (focus , unfocus field data still in it), correct value. ideas why doesn't work first time?
the 2 text inputs have ids of #frompicker
, #topicker
, jqueryui datepickers.
solution:
what did wanted this:
jquery(function() { jquery('#frompicker, #topicker').datepicker({ dateformat : 'yy-mm-dd' });
jquery('#submit').on('click', function() { var fromdate = jquery('#frompicker').val(); var todate = jquery('#topicker').val(); if (fromdate !== '' && todate !== '') { if (isvaliddate(fromdate) && isvaliddate(todate)) { // nothing } else { alert('you must enter dates in format "yyyy-mm-dd"'); return false; } } else { return false; } }); }); function isvaliddate(dt) { if (dt.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/)) { return true; } } </script>
i dont see reason code not work instead of blur
event, try onselect
event of datepicker instead
jquery('#topicker').datepicker( { onselect: function(date) { alert(date); // other stuff }, // .... );
Comments
Post a Comment