我有一个字段,您可以在其中输入您的姓名并提交。我想接收人们的名字和姓氏,为此我需要检查该值是否至少包含 2 个单词。这是我现在正在使用的,但它似乎不起作用。
function validateNameNumber(name) {
var NAME = name.value;
var matches = NAME.match(/\b[^\d\s]+\b/g);
if (matches && matches.length >= 2) {
//two or more words
return true;
} else {
//not enough words
return false;
}
}
最佳答案
str.trim().indexOf(' ') != -1 //there is at least one space, excluding leading and training spaces
关于javascript 检查值是否至少有 2 个或更多单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25344603/