[英]Regular expression for LastName, FirstName with space
I am using the below JS to enter name in LastName, FirstName format like this Clark, Michael
and now I also probably need to allow names like this Tim Duncan, Vince Carter
where these kinda names have space in between. 我正在使用下面的JS以LastName,FirstName格式输入名称,例如
Clark, Michael
,现在,我可能还需要允许Tim Duncan, Vince Carter
这样的名称,这些名称之间要有空格。
function validateName(txtbox) {
var name = /^\w{1,20}\, \w{1,20}$/
var check = document.getElementById(txtbox);
if (check != null) {
if (check.value != "") {
if (!name.test(check.value)) {
alert('Please enter name in Last Name, First Name format');
document.getElementById(txtbox).focus();
return false;
}
else {
return true;
}
}
else if (name.test(check.value)) {
return true;
}
else if (check.value == "") {
return true;
}
}
}
Is there any way to achieve this by a change within the same Regex. 有没有办法通过在同一个正则表达式中进行更改来实现这一目标。 Really appreciate suggestions and help.
非常感谢您的建议和帮助。
^[\w ]{1,20}\, [\w ]{1,20}$
这应该为您工作。
Maybe this will meet your expectations. 也许这会满足您的期望。
/^\\s*(\\w{1,20} *[^,]*)+,\\s+(\\w{1,20}\\s*)+$/
However above regex will accept also multiple white spaces before and after name. 但是,上面的正则表达式还将在名称前后接受多个空格。 If you want to force only one space character format you should use these regex:
如果只想强制使用一种空格字符格式,则应使用以下正则表达式:
/^(\\w{1,20} ?[^,]*)+, (\\w{1,20}( [^$]|$))+$/
您可以使用html5模式。
<input type="text" title="Following format: FirstName LastName" pattern="^\\s*([\\w]{1,20}( |,)?)+\\s*,\\s+([\\w]{1,20} *)+$" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.