67

How to check if a string contains [a-zA-Z] characters only?

Example:

var str = '123z56';
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
PHP Noob
  • 1,597
  • 3
  • 24
  • 34
  • 1
    an internationalized version of this question http://stackoverflow.com/questions/2013451/test-if-string-contains-only-letters-a-z-%C3%A9-%C3%BC-%C3%B6-%C3%AA-%C3%A5-%C3%B8-etc – Adriano Sep 16 '14 at 14:47

6 Answers6

119

No jQuery Needed

if (str.match(/[a-z]/i)) {
    // alphabet letters found
}
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • 1
    I'm a newbie with this stuff, so `match(/[a-z]/i)` will return `true` or `false`? And what is the `i` in the middle of it for? – user3050478 Aug 16 '17 at 14:10
  • 2
    the `i` flag is for case insensitive. The `match` function returns an array of the matches. If there are no matches, it returns an empty array, which is equivalent to `false` (as long as you are not using triple equals signs.) – Skeets Oct 10 '17 at 16:41
47

You can use regex:

/[a-z]/i.test(str);

The i makes the regex case-insensitive. You could also do:

/[a-z]/.test(str.toLowerCase());
Blender
  • 289,723
  • 53
  • 439
  • 496
  • This has the added benefit that you can test against a variable that holds a digit as well; `var a = 1; /[a-z]/i.test(a) returns false`. While `a.match` is an undefined method. – Håvard Geithus Mar 06 '16 at 17:45
11

Ahh, found the answer myself:

if (/[a-zA-Z]/.test(num)) {
  alert('Letter Found')
}
PHP Noob
  • 1,597
  • 3
  • 24
  • 34
9

I'm surprised that the answers here got so many upvotes when none of them really answer the question. Here's how to make sure that ONLY LATIN characters are in a given string.

const hasOnlyLetters = !!value.match(/^[a-z]*$/i);

The !! takes transforms something that's not boolean into a boolean value. (It's exactly the same as applying a ! twice, and in fact you can use as many ! as you'd like to toggle the truthiness multiple times.)

As for the RegEx, here's the breakdown.

  • /.../i The delimiter is a / and the i means to assess the statement in a case-insensitive fashion.
  • ^...$ The ^ means to look at the very beginning of a string. The $ means to look at the end of the string, and when used together, it means to consider the entire string. You can add more to the RegEx outside of these boundaries for things like appending/prepending a required suffix or prefix.
  • [a-z]*This part says to look for all lowercase letters. (The case-insensitive modifier means that we don't need to look at uppercase letters, too.) The * at the end says that we should match whats in the brackets any number of times. That way "abc" will match instead of just "a" or "b", and so forth.
Rick Mac Gillis
  • 754
  • 8
  • 16
5

There is no jquery needed:

var matchedPosition = str.search(/[a-z]/i);
if(matchedPosition != -1) {
    alert('found');
}
SomeoneYouDontKnow
  • 1,289
  • 10
  • 15
1

All these answers are correct, but I had to also check if the string contains other characters and Hebrew letters so I simply used:

if (!str.match(/^[\d]+$/)) {
    //contains other characters as well
}
Božo Stojković
  • 2,893
  • 1
  • 27
  • 52
Jake Neumann
  • 372
  • 4
  • 13