开发者

jQuery: Make specific keycode's acceptable for an input

开发者 https://www.devze.com 2023-04-05 23:16 出处:网络
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.

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

精彩评论

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