malcoauri
malcoauri

Reputation: 12199

JS check if string contains only cyrillic symbols and spaces

I have some string and I need to check if this string:

a) consists of 3 words b) contains ONLY cyrillic symbols and spaces

My code:

var isValid;
isValid = function(s) {
  return s && s.split(" ").length === 3 && /[а-яА-Я ]/.test(s);
};

But this code doesn't work, because isValid('a b c') returns 'true'. What is my mistake? Thanks in advance!

Upvotes: 4

Views: 3908

Answers (1)

Ty Kroll
Ty Kroll

Reputation: 1395

Try this:

var isValid = function(s) {
    return s && s.split(" ").length === 3 && /^[\u0400-\u04FF ]+$/.test(s);
};

Upvotes: 7

Related Questions