javascript - Input should allow numbers and dash character - Stack Overflow

I have written jquery for allowing numbers and dash - from being entered$('.no-special-characters&

I have written jquery for allowing numbers and dash - from being entered

$('.no-special-characters').keydown(function(e){  
  if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode == 45) {
    return true; 
  }  else {
    return false;
  } 
});

It does not work accordingly. It allows only numbers to be accepted.

I have written jquery for allowing numbers and dash - from being entered

$('.no-special-characters').keydown(function(e){  
  if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode == 45) {
    return true; 
  }  else {
    return false;
  } 
});

It does not work accordingly. It allows only numbers to be accepted.

Share Improve this question edited Sep 20, 2017 at 11:03 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Sep 20, 2017 at 10:57 NidaNida 1,7023 gold badges36 silver badges75 bronze badges 1
  • Just as a side note, if you want to control what's in an input, you should check characters instead of the keyboard event. Because there are ways to enter characters in an input without explicitly typing it, for example pasting in the input. – Kewin Dousse Commented Sep 20, 2017 at 11:00
Add a ment  | 

4 Answers 4

Reset to default 2

try this code

$('.no-special-characters').keydown(function(e) {
  if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode == 189) {
    return true;
  } else {
    return false;
  }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="no-special-characters">

Try this

Updated with backspace support

Allow the keycode of 189

$('.no-special-characters').keydown(function(e) {
   var key =  e.keyCode|e.which;
      console.log(key) //check the key value in your console.log
      if (key >= 48 && key <= 57 || key == 45 || key == 189 ||key == 8){
          return true;
        } else {
          return false;
        }
      });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="no-special-characters">

Here you go with one more solution

$('.no-special-characters').keydown(function(e){  
  if ((e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode == 189) {
    return true; 
  } else {
    return false;
  } 
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="no-special-characters" type="text" />

Usually bine the keyCode from 48 to 57 & then the next keyCode condition.

Hope this will help you.

e.keyCode = 109 is '-' on numpad

e.keyCode = 189 is '-' in alphabate keybord key on chrome

e.keyCode = 173 is '-' in alphabate keyboard key on firefox & on chrome 173 keycord is Mute On|Off

Source

Maybe this helps you, because using only e.keyCode == 189 (as some answers say) wont work in Firefox.

You can see, what keyCode your key presses return here: Link

Edit: You can also use regular expressions. Then there is no need to add keyCodes for different browsers:

$('.no-special-characters').keypress(function(e){  
    var txt = String.fromCharCode(e.which)
    var pattern = /^[0-9\-]+$/;
    if (!(pattern.test(txt) || e.keyCode == 8)){
            return false;
    }
})

JSFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745521716a4631289.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信