我正在使用下面的 JS 以这样的姓氏、名字格式输入姓名 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;
}
}
}
有没有办法通过在同一个正则表达式中进行更改来实现这一点。非常感谢建议和帮助。
最佳答案
^[\w ]{1,20}\, [\w ]{1,20}$
这应该适合你。
关于javascript - 带空格的姓氏、名字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27816630/