开发者

jQuery: Make specific keycode's acceptable for an input

How can I make a custom validator for an input which accepts only numbers, backspace and full stop. That's keyCode's 47-59, 8 and 190.

I would image it would be开发者_StackOverflow something like:

$('input').keyup(function (e) {

     if NOT (e.keyCode == 48 TO 59 && e.keyCode == 8 && e.keyCode == 190) {
          return false;
     }

});

I also want to fadeIn and Out an added class on the input. For example class="red" The .red then will have a red border to show that it's not possible to type letters.

Something like: $('input').addClass("red") with a fadeIn, fadeOut effect.

Thanks alot


if ( e.keyCode !== 8 && e.keyCode !== 190 && ( e.keyCode < 48 || e.keyCode > 59 ) ) {
   return false;
}

You will need the color plugin to do the red fade.

$( "input" ).animate({
  color: "red"
}, "fast");


I found this to work:

var e = event || evt;
if (e.keyCode > 31 && e.keyCode !== 190 && (e.keyCode < 48 || e.keyCode > 57))
   return false;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜