javascript 仅禁用输入字段中的 .(DOT) 特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31072376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:13:11  来源:igfitidea点击:

Disable only .(DOT) special charecter in input field

javascriptjqueryhtml-input

提问by Vijay Kumar

I don't want allow user to enter. (Dot) in my input field.. Rest of the required validation done already.

我不想让用户进入。(点)在我的输入字段中.. 其余所需的验证已经完成。

function isNumberKey(evt) {
  var charCode = (evt.which) ? evt.which : event.keyCode;
  if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

  return true;
}
<input type="text" class="form-control" name="ContactNumber" maxlength="10" value="" id="ContactNumber" title="Enter Number Here" onkeypress="return isNumberKey(event)" required>

回答by Pratik Galoria

$("#name").on("keypress", function(evt) {
  var keycode = evt.charCode || evt.keyCode;
  if (keycode == 46) {
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="name" />

Working example here: jsfiddle

这里的工作示例:jsfiddle

回答by Identity1

In jQuery:

在 jQuery 中:

jQuery.validator.addMethod("someRule", function(string, element) { 
    return !string.match(/\./g);
}, "Your username contains a dot!");

In HTML use the patternattribute:

在 HTML 中使用pattern属性:

pattern="[^.]"