Angel
Angel

Reputation: 357

Regular expression that validates only alphabetical characters

I have this function:

function validate(str1,str2,str3){
  var alph=/^[A-Za-zÑñ]*$/;

  if((str1.match(alph) || str2.match(alph) || str3.match(alph))){
    return true;    
  }else{
    return false;
  }
}

If I enter any alphabetical character in my input text it returns true. BUT, when I put any number, it returns true too.

I need to accept only alphabetical characters and spaces, nothing more.

Can anyone help me?

Upvotes: 0

Views: 185

Answers (2)

Laughing
Laughing

Reputation: 211

try this:

if((str1.match(alph) && str2.match(alph) && str3.match(alph)))

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173662

If your function is supposed to return false if any strings passed are invalid you need a logical AND:

return str1.match(alph) && str2.match(alph) && str3.match(alph);

Also, you could use alph.test() instead if you're not using the matching results:

return alph.test(str1) && alph.test(str2) && alph.test(str3);

Upvotes: 4

Related Questions