javascript检查value是否至少包含2个或更多的单词 [英] javascript check if value has at least 2 or more words
本文介绍了javascript检查value是否至少包含2个或更多的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字段,您可以在其中输入您的姓名并提交.我希望接收人的名字和姓氏,为此,我需要检查值是否至少包含2个字.这是我现在正在使用的东西,但似乎不起作用.
I have a field that you enter your name in and submit it. I would like the receive peoples first and last names and to do this i need to check if the value contains at least 2 words. This is what I am using right now but it doesn't seem to work.
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检查value是否至少包含2个或更多的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文