最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论