Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript check if value has at least 2 or more words

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;
    }
}
like image 706
Bryce Hahn Avatar asked Aug 16 '14 22:08

Bryce Hahn


People also ask

How to check if string contain number JavaScript?

Use the RegExp. test() method to check if a string contains at least one number, e.g. /\d/. test(str) . The test method will return true if the string contains at least one number, otherwise false will be returned.

What is $$ in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.

How do you check if the number is less than 1 in JavaScript?

You have to use if (isNaN(x)) . Use a while() loop and break out of it when the input meets the criteria.


Video Answer


1 Answers

str.trim().indexOf(' ') != -1 //there is at least one space, excluding leading and training spaces
like image 120
simonberry Avatar answered Oct 08 '22 22:10

simonberry