Javascript 允许用户在输入字段中只输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27100846/
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
Allow user type only text in input field
提问by Niket Malik
I need a validator that allows user type only textnumberand @
我需要一个验证器,允许用户类型只textnumber和@
when user types other than these charter like #, $, % ,^ , &, *, (,these charter should not type in input field is there any way to block other than allowed charter inside input field
当用户键入#, $, % ,^ , &, *, (,这些章程以外的内容时,不应在输入字段中输入这些章程,除了允许的章程在输入字段中,还有什么方法可以阻止
回答by Niket Malik
You are probably looking for regex. Try testing the string on keyUp event on the below regexp:
您可能正在寻找正则表达式。尝试在下面的正则表达式上测试 keyUp 事件上的字符串:
^[a-zA-Z0-9@]+$
CODE:
代码:
// This will be triggered everytime a user types anything
// in the input field with id as input-field
$("#input-field").keyup(function(e) {
// Our regex
// a-z => allow all lowercase alphabets
// A-Z => allow all uppercase alphabets
// 0-9 => allow all numbers
// @ => allow @ symbol
var regex = /^[a-zA-Z0-9@]+$/;
// This is will test the value against the regex
// Will return True if regex satisfied
if (regex.test(this.value) !== true)
//alert if not true
//alert("Invalid Input");
// You can replace the invalid characters by:
this.value = this.value.replace(/[^a-zA-Z0-9@]+/, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input-field" />
UPDATE:
更新:
Without jquery:
没有jQuery:
function validate() {
var element = document.getElementById('input-field');
element.value = element.value.replace(/[^a-zA-Z0-9@]+/, '');
};
<input type="text" id="input-field" onkeyup="validate();" />
回答by Niket Malik
You may try with RegEx
您可以尝试使用 RegEx
$(function() {//<-- wrapped here
$('.restrict').on('input', function() {
this.value = this.value.replace(/[^a-zA-Z0-9@]/g, ''); //<-- replace all other than given set of values
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' class='restrict'>
EDIT
编辑
With pure js
用纯js
var restrict = function(tb) {
tb.value = tb.value.replace(/[^a-zA-Z0-9@]/g, '');
};
<input type='text' onpaste="restrict(this);" onkeypress="restrict(this);" onkeyup="restrict(this);">
<input type='text' onpaste="restrict(this);" onkeypress="restrict(this);" onkeyup="restrict(this);">
回答by Moni
Try below code for javascript validation-
尝试以下代码进行 javascript 验证-
function validate(inputtxt)
{
var letterNumber = /^[0-9a-zA-Z@]+$/;
if((inputtxt.value.match(letterNumber))
{
return true;
}
else
{
alert("message");
return false;
}
}
and try to put this line in your html code, replace the name of your form or input field-
并尝试将此行放在您的 html 代码中,替换您的表单或输入字段的名称-
<input type="submit" name="submit" value="Submit" onclick="validate(document.form.text)" />

