jquery - Time Validation in Javascript -


i'm working on time , validation in javascript, in validation time entered user , validate through conditions, date gets validate time not getting validate code :

html

<input type="date" id="date-input" required /> <input type="time" id="stime" required/> <input type="time" id="etime" required/> <button id="submit">submit</button> 

javascript

$('#submit').on('click', function(){ //date validation ------------------------------------ var date = new date($('#date-input').val());   var today = new date();   var date1 = date.getdate();   var date2 = today.getdate();   if(date1 < date2){   alert("please fill valid date");   } //-------------------time validation----------------------- var t1 = new date($('#stime').val()); var t2 = new date($('#etime').val()); var time1 = t1.gethours(); var time2 = t2.gethours();  if(time1==time2){ alert("please make differrnce between time"); }  if (time1 < 9 || time2 > 17 ){ alert("please fill time between 9:00 17:00"); } 

try time in miliseconds , compare them in miliseconds validate hours.

var t_one= $('#stime').val(); var t1 = t_one.split(":"); var t_two = $('#etime').val(); var t2 = t_two.split(":"); var time1 = (+t1[0] * (60000 * 60)) + (+t1[1] * 60000); var time2 = (+t2[0] * (60000 * 60)) + (+t2[1] * 60000); 

you can see working here.

https://jsbin.com/xagisikado/edit?html,js,console,output


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -