Check if string contains only alphabets javascript

So I tried this:

if (/^[a-zA-Z]/.test(word)) {
   // code
}

It doesn't accept this : " "

But it does accept this: "word word", which does contain a space :/

Is there a good way to do this?

Check if string contains only alphabets javascript

Felix Kling

768k171 gold badges1068 silver badges1114 bronze badges

asked May 5, 2014 at 15:43

Check if string contains only alphabets javascript

2

With /^[a-zA-Z]/ you only check the first character:

  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:
    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"

If you want to check if all characters are letters, use this instead:

/^[a-zA-Z]+$/.test(str);
  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:
    • +: Between one and unlimited times, as many as possible, giving back as needed (greedy)
    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"
  • $: Assert position at the end of the string (or before the line break at the end of the string, if any)

Or, using the case-insensitive flag i, you could simplify it to

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

Or, since you only want to test, and not match, you could check for the opposite, and negate it:

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

mohagali

1932 silver badges9 bronze badges

answered May 5, 2014 at 15:45

OriolOriol

256k57 gold badges407 silver badges493 bronze badges

0

The fastest way is to check if there is a non letter:

if (!/[^a-zA-Z]/.test(word))

answered May 5, 2014 at 15:50

Check if string contains only alphabets javascript

2

You need

/^[a-zA-Z]+$/

Currently, you are matching a single character at the start of the input. If your goal is to match letter characters (one or more) from start to finish, then you need to repeat the a-z character match (using +) and specify that you want to match all the way to the end (via $)

answered May 5, 2014 at 15:46

Check if string contains only alphabets javascript

JDBJDB

23.9k5 gold badges71 silver badges117 bronze badges

Try this

var Regex='/^[^a-zA-Z]*$/';

 if(Regex.test(word))
 {
 //...
 }

I think it will be working for you.

answered Sep 22, 2017 at 6:55

1

try to add \S at your pattern

^[A-Za-z]\S*$

answered May 5, 2014 at 15:46

Check if string contains only alphabets javascript

ToninoTonino

1,01010 silver badges22 bronze badges

Check if String contains only Letters #

Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/.test(str). The test method will return true if the string contains only letters and false otherwise.

Copied!

function onlyLetters(str) { return /^[a-zA-Z]+$/.test(str); } console.log(onlyLetters('hello')); // 👉️ true console.log(onlyLetters('hello123')); // 👉️ false console.log(onlyLetters('one,two')); // 👉️ false

If you also need to match spaces, dots, commas, etc, scroll down to the next code snippet.

We used the RegExp.test method to check if a string contains only letters.

The only parameter the method takes is a string against which the regular expression is matched.

The test method returns true if the string is matched in the regex and false otherwise.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

The part between the square brackets [] is called a character class and matches a range of lowercase a-z and uppercase A-Z letters.

The plus + matches the preceding item (the letter ranges) 1 or more times.

If you ever need help reading a regular expression, check this regex cheatsheet from MDN out.

If you also need to match dots, commas or spaces, etc, add the character you need to match between the square brackets [].

Copied!

function onlyLettersSpacesDots(str) { return /^[a-zA-Z\s.,]+$/.test(str); } console.log(onlyLettersSpacesDots('hello world')); // 👉️ true console.log(onlyLettersSpacesDots('hello.world')); // 👉️ true console.log(onlyLettersSpacesDots('hello,world')); // 👉️ true console.log(onlyLettersSpacesDots('hello123')); // 👉️ false

In this example we match letters, whitespace characters (\s), dots and commas.

The \s special character matches spaces, tabs and new lines.

You can update the characters between the square brackets according to your use case.

Further Reading #

  • Check if String contains only Spaces in JavaScript
  • Check if String contains only Digits in JavaScript

How do you check if a string only contains alphabets in JavaScript?

Use the test() method to check if a string contains only letters, e.g. /^[a-zA-Z]+$/. test(str) . The test method will return true if the string contains only letters and false otherwise. Copied!

How do I check if a string contains only characters?

Java For Testers Read the String. Convert all the characters in the given String to lower case using the toLower() method. Convert it into a character array using the toCharArray() method of the String class. Find whether every character in the array is in between a and z, if not, return false.

How do you check if a string contains only alphabets and spaces in Java?

We can use the regex ^[a-zA-Z]*$ to check a string for alphabets. This can be done using the matches() method of the String class, which tells whether the string matches the given regex.

How do you check if a string contains a substring in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.