Javascript 用于名称验证的正则表达式只允许字母和空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41975496/
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
Regex for names validation allow only letters and spaces
提问by claudios
I'm trying to validate using jQuery Validator if input only contains spaces, and allow space only if there's a letter inside it.
如果输入仅包含空格,我正在尝试使用 jQuery Validator 进行验证,并且仅当其中有字母时才允许使用空格。
Note that it should also display error if name contains numbers. Allow only the space if it starts with a letter.
请注意,如果名称包含数字,它也应该显示错误。仅允许以字母开头的空格。
This is what I have so far that only allows letters and spaces:
这是我到目前为止只允许字母和空格的内容:
jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "letters only");
Also tried this one but it trims the string and can't add a space between names:
也试过这个,但它修剪了字符串并且不能在名称之间添加空格:
first_name : {
letterswithspace : true,
required: {
depends:function(){
$(this).val($.trim($(this).val()));
return true;
}
}
}
回答by Jaqen H'ghar
To match the terms:
要匹配条款:
- Expression can start only with a letter
- Expression can contain letters or spaces
- 表达式只能以字母开头
- 表达式可以包含字母或空格
You need to use this regex expression:
您需要使用此正则表达式:
/^[a-z][a-z\s]*$/
So in your js it should be:
所以在你的 js 中它应该是:
jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-z][a-z\s]*$/i.test(value);
}, "letters only");
Explanation
解释
^[a-z]means start with one letter[a-z\s]*$means after accept zero or more letters or spaces
^[a-z]表示以一个字母开头[a-z\s]*$表示接受零个或多个字母或空格后
Valid sentence
有效句子
If you want a valid sentence structure:
如果你想要一个有效的句子结构:
- Expression can start or end only with a letter
- Expression cannot contain consecutive spaces
- 表达式只能以字母开头或结尾
- 表达式不能包含连续空格
use:
用:
/^([a-z]+\s)*[a-z]+$/
By the way
顺便一提
- These regex expressions do not accept capital letters. To add capital letters support instead of
a-zusea-zA-Z
- 这些正则表达式不接受大写字母。添加大写字母支持而不是
a-z使用a-zA-Z
回答by Dhiren Patel
You need to use this regex expression:-
您需要使用此正则表达式:-
/^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/;
Example Without Jquery Validator using Javascript:- Its Work Fine Javascript Example
没有使用 Javascript 的 Jquery 验证器的示例:- 其工作正常Javascript 示例
Hope Its Work !!
希望它的工作!
回答by Shubhanshu Nishad
Jquery validation for Full Name using regular expression
使用正则表达式对全名进行 Jquery 验证
Validation includes:
验证包括:
Name not allow numbers.
Name allow alphabets only,not allow any special characters.
<html> <body> <label>Full Name:</label> <input type="text" class="form-control name-valid"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $('.name-valid').on('keypress', function(e) { var regex = new RegExp("^[a-zA-Z ]*$"); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; } e.preventDefault(); return false; }); }); </script> </body> </html>
名称不允许数字。
名称只允许字母,不允许任何特殊字符。
<html> <body> <label>Full Name:</label> <input type="text" class="form-control name-valid"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $('.name-valid').on('keypress', function(e) { var regex = new RegExp("^[a-zA-Z ]*$"); var str = String.fromCharCode(!e.charCode ? e.which : e.charCode); if (regex.test(str)) { return true; } e.preventDefault(); return false; }); }); </script> </body> </html>
For Demo Click here!
演示请点击这里!
回答by methababu
jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-zA-Z ]*$/.test(value); }, "letters only");

