我想使用jQuery实现只允许输入字母(a-z)的文本框。有什么示例吗?
<input name="lorem" onkeyup="this.value=this.value.replace(/[^a-z]/g,'');">
对于那些喜欢复制粘贴而非手动输入的恶意用户,onfocus
和 onblur
可以被用来实现相同的功能;)
[+] 美观的jQuery代码:
<input name="lorem" class="alphaonly">
<script type="text/javascript">
$('.alphaonly').bind('keyup blur',function(){
var node = $(this);
node.val(node.val().replace(/[^a-z]/g,'') ); }
);
</script>
$(".alpha-only").on("keydown", function(event){
// Allow controls such as backspace, tab etc.
var arr = [8,9,16,17,20,35,36,37,38,39,40,45,46];
// Allow letters
for(var i = 65; i <= 90; i++){
arr.push(i);
}
// Prevent default if not in array
if(jQuery.inArray(event.which, arr) === -1){
event.preventDefault();
}
});
请注意,这允许使用大写和小写字母。
我已经包含了诸如退格、删除和箭头键等关键代码。您可以使用此密钥代码列表作为参考来创建自己的数组。
当然,用户仍然可以粘贴非字母字符(例如通过CTRL+V或右键单击),因此我们仍然需要使用.on("input"...
监视所有更改,但replace()
仅在必要时:
$(".alpha-only").on("input", function(){
var regexp = /[^a-zA-Z]/g;
if($(this).val().match(regexp)){
$(this).val( $(this).val().replace(regexp,'') );
}
});
这意味着当用户粘贴非字母字符时,我们仍然会出现光标跳到末尾的不良效果。
为了解决这个问题,我建议使用type="url"
,因为URL可以接受大小写字母但不会被自动更正。然后,为了避免浏览器尝试验证URL,您必须在form
标记中使用novalidate
。
%20
),所以您只需要在正则表达式中的Z
后添加一个空格即可。 - rybo111// allow only Alphabets A-Z a-z _ and space
$('.alphaonly').bind('keyup blur',function(){
var node = $(this);
node.val(node.val().replace(/[^A-Za-z_\s]/,'') ); } // (/[^a-z]/g,''
);
// allow only Number 0 to 9
$('.numberonly').bind('keyup blur',function(){
var node = $(this);
node.val(node.val().replace(/[^0-9]/,'') ); } // (/[^a-z]/g,''
);
$(function() {
$('#txtFirstName').keydown(function(e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input id="txtFirstName" value="">
$("#test").keypress(function(event){
var inputValue = event.charCode;
//alert(inputValue);
if(!((inputValue > 64 && inputValue < 91) || (inputValue > 96 && inputValue < 123)||(inputValue==32) || (inputValue==0))){
event.preventDefault();
}
});
$("#test1").keypress(function(event){
var inputValue = event.charCode;
//alert(inputValue);
if(!((inputValue > 47 && inputValue < 58) ||(inputValue==32) || (inputValue==0))){
event.preventDefault();
}
});
$("#test3").keypress(function(event){
var inputValue = event.charCode;
//alert(inputValue);
if(!((inputValue > 64 && inputValue < 91) || (inputValue > 96 && inputValue < 123)||(inputValue==32)||(inputValue > 47 && inputValue < 58) ||(inputValue==32) || (inputValue==0))){
event.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
For letters:<input type="text" id="test"> <br>
<br>
For Numbers: <input type="text" id="test1">
<br>
<br>
For Alphanumeric: <input type="text" id="test3">
@dev-null-dweller
所描述的解决方案是完全有效的。jQuery 3.0
开始,.bind()
方法已被弃用。自jQuery 1.7
以来,它已被.on()
方法取代,用于将事件处理程序附加到文档,因此其使用已经不建议了。.on()
方法替代.bind()
。$('.alphaonly').on('keyup blur', function(){
var node = $(this);
node.val( node.val().replace(/[^a-z]/g,'') );
});
$(document).on('keyup blur', '.alphaonly', function(){
var node = $(this);
node.val(node.val().replace(/[^a-z]/g,'') );
});
感谢第一个回答..做到了这个..
<input name="lorem" class="alpha-only">
<script type="text/javascript">
$(function()
{
$('.alpha-only').bind('keyup input',function()
{
if (this.value.match(/[^a-zA-Z áéíóúÁÉÍÓÚüÜ]/g))
{
this.value = this.value.replace(/[^a-zA-Z áéíóúÁÉÍÓÚüÜ]/g, '');
}
});
});
</script>
这个版本有一些改进,比如支持带重音的字母,将“模糊”改为“输入”可以纠正短暂显示非字母字符的问题,同时也修复了使用鼠标选择文本时出现的拖动问题。
支持退格键:
new RegExp("^[a-zA-Z \b]*$");
此选项不会检查移动设备。因此,您可以使用jQuery Mask Plugin并使用以下代码:
jQuery('.alpha-field, input[name=fname]').mask('Z',{translation: {'Z': {pattern: /[a-zA-Z ]/, recursive: true}}});
JQuery函数,只允许输入小写和大写字母:
文本框:
<input id="a" type="text" />
JQuery 函数:
$('#a').keydown(function (e) {
if (e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
jQuery(this).val(jQuery(this).val().replace(/[^a-z]/g,'') ); } ); - Mahder