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.
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} *)+$" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.