In my form I have a field which asks the user what time they would like me to call them.
The code for the input is as follows (excluding the <form> tags):
<p>
<label>When can we call you?:</label>
<br>
<input type="text" name="contactconvenience" placeholder="HH:MM or "anytime"" onchange="checkCallTime()">
</p>
<div id="callbackwarn"></div>
I want to use a RegEx to check whether the user has entered a time in the format HH:MM or the specific string "anytime", but the 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 a time in the format HH:MM or type \"anytime\".";
document.bookingsform.contactconvenience.title = "Please enter a time in the 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 you can see I have use two separate RegEx expressions, one for matching a time in the format HH:MM; and the other for matching the specific string "anytime".
In the function itself, I'm using the || to see if either of the RegExs have been matched or not.
NB: I am aware that I'm using the onchange rather than onsubmit, the <form> tag consists of the onsubmit calling on a master function.