JS功能仅允许输入字母和空格 [英] JS function to allow enter only letters and white spaces
本文介绍了JS功能仅允许输入字母和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个jquery或js函数仅允许输入字母和空格. 预先感谢.
I need a jquery or js function to only allow enter letters and white spaces. Thanks in advance.
页面:
<p:inputText onkeypress="onlyLetter(this)">
功能:
function onlyLetter(input){
$(input).keypress(function(ev) {
var keyCode = window.event ? ev.keyCode : ev.which;
// code
});
}
推荐答案
只需使用您要禁用或禁止使用的键/数字的ascii代码(十进制值). ASCII表 .
Just use ascii codes (decimal values) of keys/digits that you want to disable or prevent from being work. ASCII Table .
HTML:
<input id="inputTextBox" type="text" />
jQuery:
$(document).ready(function(){
$("#inputTextBox").keypress(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
});
这篇关于JS功能仅允许输入字母和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文