javascript - Validating date input field with onsubmit -
i looking everywhere, couldn't find code problem. have input fields, users using send online requests. , 1 of fields date field, somehow validate in way, allowed if entered date somewhere between today , 8 days before. example: if today 29 november , allowed enter date between 21st-29th november , nothing else , shown alert window when have entered wrong date pick days minicalendar part resolved, need validating. if post working code, grateful. thank you
use jquery ui datepicker, script below:
$(function() { var currentdate = new date(); var maxalloweddate = new date(currentdate); maxalloweddate.setdate(currentdate.getdate() + 8); $( "#datepicker" ).datepicker({ changeyear: true, mindate: '0', maxdate: '+7d', }); $('#datepicker').change(function(){ var enteredval = new date(this.value); if(enteredval.gettime() < currentdate.gettime() || enteredval.gettime() > maxalloweddate.gettime()) { alert("invalid"); } else { alert("valid"); } }); });
and ui:
<div class="demo"> <p>date: <input type="text" id="datepicker"></p>
here jsfiddle demo: http://jsfiddle.net/pjkz7k0t/1/
Comments
Post a Comment