如何在输入文本字段中只允许字符串
How to allow only string in input text field
如何使用 rails haml 表单在 text_field 上仅允许输入字符串。
.field
= f.label "agent_name"
= f.text_field :agent_name, :required => true,:id=>"agent_name_validation"
$("#agent_name_validation").keypress(function(event) {
var string = /^[a-z]+$/i;
if(event.which != string){
return false;
}
});
使用以下 Jquery 函数从文本字段中删除数字
$("#agent_name_validation").keyup(function(e) {
// Our regex
// a-z => allow all lowercase alphabets
// A-Z => allow all uppercase alphabets
var regex = /^[a-zA-Z]+$/;
// 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-Z]+/, '');
});
使用这个脚本
$("#agent_name_validation").keypress(function(event){
var inputValue = event.charCode;
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)){
event.preventDefault();
}
});
您可以使用 RegExp.prototype.test() to remove the entered character if that is not a string in between the regular expression [a-z]
:
请注意,这将只检查小写字符:
$('#agent_name_validation').on('input', function () {
if (!/[a-z]$/.test(this.value)) {
this.value = this.value.slice(0, -1);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="agent_name_validation" type="text">
尝试添加jQuery验证gem
https://github.com/meowsus/jquery-validation-rails
太多简单的方法来进行大量验证
所有验证都可以使用,您只需使用
他们现成的方法
myField: { lettersonly: true }
完成了..
祝你好运..
@苏曼达斯
试试这个代码,只允许输入文本字段中的字符串:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Javascript Demo</title>
</head>
<body>
<form action="/data.php" method="get">
<input type="text" name="fullName" onkeypress="return (event.charCode > 64 &&
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)"
placeholder="Full Name">
<input type="submit">
</form>
</body>
</html>
希望以上代码对你有用。
谢谢。
如何使用 rails haml 表单在 text_field 上仅允许输入字符串。
.field
= f.label "agent_name"
= f.text_field :agent_name, :required => true,:id=>"agent_name_validation"
$("#agent_name_validation").keypress(function(event) {
var string = /^[a-z]+$/i;
if(event.which != string){
return false;
}
});
使用以下 Jquery 函数从文本字段中删除数字
$("#agent_name_validation").keyup(function(e) {
// Our regex
// a-z => allow all lowercase alphabets
// A-Z => allow all uppercase alphabets
var regex = /^[a-zA-Z]+$/;
// 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-Z]+/, '');
});
使用这个脚本
$("#agent_name_validation").keypress(function(event){
var inputValue = event.charCode;
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)){
event.preventDefault();
}
});
您可以使用 RegExp.prototype.test() to remove the entered character if that is not a string in between the regular expression [a-z]
:
请注意,这将只检查小写字符:
$('#agent_name_validation').on('input', function () {
if (!/[a-z]$/.test(this.value)) {
this.value = this.value.slice(0, -1);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="agent_name_validation" type="text">
尝试添加jQuery验证gem https://github.com/meowsus/jquery-validation-rails 太多简单的方法来进行大量验证 所有验证都可以使用,您只需使用 他们现成的方法
myField: { lettersonly: true }
完成了.. 祝你好运..
@苏曼达斯
试试这个代码,只允许输入文本字段中的字符串:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Javascript Demo</title>
</head>
<body>
<form action="/data.php" method="get">
<input type="text" name="fullName" onkeypress="return (event.charCode > 64 &&
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)"
placeholder="Full Name">
<input type="submit">
</form>
</body>
</html>
希望以上代码对你有用。
谢谢。