开发者

Cancel a keystroke in jQuery

开发者 https://www.devze.com 2023-01-01 22:14 出处:网络
Is is possible (cross-browser compatible) to CANCEL a keystroke after a user h开发者_运维知识库as made it (for example on a textbox)

Is is possible (cross-browser compatible) to CANCEL a keystroke after a user h开发者_运维知识库as made it (for example on a textbox)

The code I currently use edits the textbox value after the keystroke has been displayed:

$('.number').keypress(function() {
    this.value = this.value.replace(/[^0-9\.]/g, '');
});


$('.number').keypress(function() {
    if ( this.value == 'foobar' ){
        // "Cancel" keystroke
        return false;
    }
    this.value = this.value.replace(/[^0-9\.]/g, '');
});


SOLVED (AND UPDATED)

Apologies, I didnt test the most obvious option - which worked:

$('.number').keypress(function(event) {
    return /[0-9\.]/.test(String.fromCharCode(event.keyCode));
});
0

精彩评论

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