How can i validate that a text field with time is entered in 15 minute increments using jquery? -
i trying validate user has entered time in text field in 15 minute increments. 8:00, 8:15, 8:30am or 8:45a valid. tried using :contains doesn't seem working using or comparison.
here fiddle: http://jsfiddle.net/fuelishways/h0tmvqyc/4/
html:
<form method="post"> <label>arrival time</label> <input type="text" id="arrivaltime"/> <br/> <label>event start time</label> <input type="text" id="eventstarttime"/> <br/> <label>event end time</label> <input type="text" id="eventendtime"/> <br/> <label>departure time</label> <input type="text" id="departuretime"/> <br/> <input type="submit"/> </form>
jquery:
$("form").submit(function(){ var arrivaltime = $('#arrivaltime').val(); var eventstarttime = $("#eventstarttime").val(); var eventendtime = $("#eventendtime").val(); var departuretime = $("#departuretime").val(); if ( arrivaltime.indexof(':00') === -1 ) || ( arrivaltime.indexof(':15') === -1 ) || ( arrivaltime.indexof(':30') === -1 ) || ( arrivaltime.indexof(':45') === -1 ) { alert("there not :00 or :15 or :30 or :45"); return false; } alert(arrivaltime + " " + eventstarttime + " " + eventendtime + " " + departuretime); return false; // change return true when working });
the problem code using || instead of &&. code error unless contains :00 , :15 , :45 , :30
if ( arrivaltime.indexof(':00') === -1 ) && ( arrivaltime.indexof(':15') === -1 ) && ( arrivaltime.indexof(':30') === -1 ) && ( arrivaltime.indexof(':45') === -1 ) { alert("there not :00 or :15 or :30 or :45"); return false; }
although regex may better qualify it.
Comments
Post a Comment