开发者

Providing feedback in a form using jquery

开发者 https://www.devze.com 2023-03-19 17:58 出处:网络
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百科.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消