0

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 &#34;anytime&#34;" 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.

2 Answers 2

1

Assumming 00:00 for midnight;

var callback = /^(([01]\d|2[0-3]):([0-5]\d)|anytime)$/;

if (!callback.test(document.bookingsform.contactconvenience.value))
{
   // error code
}
Sign up to request clarification or add additional context in comments.

4 Comments

This isn't working, if I type in arbitrary characters it won't show the error. I removed the || and now just using your one RegEx. Any ideas?
Yep, that isn't working for me. I even copied and pasted your code, still nothing.
Works ok for me? jsfiddle.net/alexk/4AZqs (must click out of the input to raise the event)
How peculiar, I copied and pasted your jsfiddle code and now it works perfectly. There must've been some mistyped code which I failed to pick up in the error code area of the function. Works perfectly now. Thanks!
0

You have to use an AND && not an OR || between the two test:

if ((!callback.test(document.bookingsform.contactconvenience.value)) 
    && 
    (!callbackanytime.test(document.bookingsform.contactconvenience.value))) {

NOT (A OR B) <=> NOT A AND NOT B

You could also use an unique regex:

/^\d{2}:\d{2}|\s*anytime\s*$/;

4 Comments

I changed it and this didn't work. I then tried your unique RegEx and still didn't work. Any ideas?
What is the value of contactconvenience.
I think None? I haven't given the tag a value attribute.
@RoyalSwish: I meant the value when you enter the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.