Providing feedback in a form using jquery
I want to provide feedback to a user when they enter something into a textbox. A little tick if their input matches a RegEx and false if it does not开发者_JAVA百科.
What event do I need to use for this? Key up?
For example:
if (inputText.match(/regex/))
{
document.write(input is correct); // or something else
}
I want to provide them feedback as they type. Something like what Yahoo Registration does.
I'd use "keypress", which is what's generated after a keyboard activity changes the input value. Thus, "keydown" or "keyup" would tell you that they pressed the backspace key, but "keypress" is what you get after the result of the backspace key.
Use the onChange event to bind your validator.
var elem = document.getElementById('#foo');
var checkbox = document.getElementById('#bar');
elem.onchange = function() {
if(elem.value.match('/regex/'))
checkbox.class="valid";
else
checkbox.class="invalid";
}
As you want to provide feedback as the user types, you are right - you can use the keyup
(or keypress
as has been noted in other answers) event:
$("#elementID").keyup(function() {
//Your code
});
You could also use the change
or blur
events to validate the input when the user moves focus away from the element.
精彩评论