html - JavaScript - RegEx for time or a specific string -
in form have field asks user time me call them.
the code input follows (excluding <form>
tags):
<p> <label>when can call you?:</label> <br> <input type="text" name="contactconvenience" placeholder="hh:mm or "anytime"" onchange="checkcalltime()"> </p> <div id="callbackwarn"></div>
i want use regex check whether user has entered time in format hh:mm or specific string "anytime", code i'm using doesn't work.
var callback = /^\d{2}([:]\d{2})$/; var callbackanytime = str.match(/anytime/g); function checkcalltime() { var valid = true; if ((!callback.test(document.bookingsform.contactconvenience.value)) || (!callbackanytime.test(document.bookingsform.contactconvenience.value))) { document.bookingsform.contactconvenience.style.border = "1px solid red"; document.getelementbyid("callbackwarn").innerhtml = "enter time in format hh:mm or type \"anytime\"."; document.bookingsform.contactconvenience.title = "please enter time in format hh:mm or type \"anytime\"."; document.getelementbyid("callbackwarn").style.display = "block"; valid = false; } else { document.bookingsform.contactconvenience.style.border = "1px inset #ebe9ed"; document.bookingsform.contactconvenience.style.borderradius = "2px"; document.getelementbyid("callbackwarn").style.display = "none"; } }
as can see have use 2 separate regex expressions, 1 matching time in format hh:mm; , other matching specific string "anytime".
in function itself, i'm using ||
see if
either of regexs have been matched or not.
nb: aware i'm using onchange
rather onsubmit
, <form>
tag consists of onsubmit
calling on master function.
assumming 00:00 midnight;
var callback = /^(([01]\d|2[0-3]):([0-5]\d)|anytime)$/; if (!callback.test(document.bookingsform.contactconvenience.value)) { // error code }
Comments
Post a Comment