JavaScript Program to Check if a String Contains only Alphabetic Characters

In this article, we are going to learn about checking if a string contains only alphabetic characters in JavaScript.Checking if a string contains only alphabetic characters means verifying that all characters within the string are letters of the alphabet (A-Z or a-z) and do not include any digits, symbols, or other non-alphabetic characters.

Some common methods can be used to check if a string contains only alphabetic characters in JavaScript, which are listed below:

Table of Content

  • Using a Regular Expression
  • Using for-of loop
  • Using every() method and charCodeAt() method
  • Using isNaN() function
  • Using Array.prototype.filter and String.prototype.match
  • Using Array.prototype.reduce

We will explore all the above methods along with their basic implementation with the help of examples.

Using a Regular Expression

To check if a string contains only alphabetic characters in JavaScript, use the regular expression /^[a-zA-Z]+$/. It returns true if the string consists solely of letters, and false otherwise.

Syntax:

let regex = /^[a-zA-Z]+$/;

Example: In this example, the regular expression ^[a-zA-Z]+$ tests if a string consists only of alphabetic characters (letters). The code validates str1 as true and str2 as false.

Javascript
let str1 = "w3wiki";
let str2 = "Beginner123";

let regex = /^[a-zA-Z]+$/;

console.log(regex.test(str1));
console.log(regex.test(str2));

Output
true
false

Using for-of loop

In this approach, we are using for…of loop to iterate through each character in the input string. It returns true if all characters are alphabetic (A-Z or a-z), otherwise false.

Syntax:

for ( variable of iterableObjectName) {
...
};

Example: In this example, checkAlphabets function iterates through each character in the input string and returns true if all characters are alphabetic (A-Z or a-z), otherwise false.

Javascript
function checkAlphabets(input) {
    for (const char of input) {
        if (!(char >= "a" && char <= "z") &&
            !(char >= "A" && char <= "Z")) {
            return false;
        }
    }
    return true;
}

const str1 = "w3wiki";
const str2 = "Beginner123";

console.log(checkAlphabets(str1));
console.log(checkAlphabets(str2)); 

Output
true
false

Using every() method and charCodeAt() method

This approach converts the string into an array of characters using the spread operator ([...str]). It then uses the every method to check if every character has a Unicode code within the range of alphabetic characters.

Example: In this example, we are using every method and charCodeAt.

Javascript
function isAlphabetic(str) {
    const result = [...str]
        .every(char => (char >= 'a' && char <= 'z') 
                    || (char >= 'A' && char <= 'Z'));
    console.log(result);
    return result;
}

// Example usage
isAlphabetic("abc"); // true
isAlphabetic("123"); // false

Output
true
false

Using isNaN() function

In this Approach we iterates through each character of the string, checking if it’s not a number using isNaN(). It returns true if all characters are non-numeric, indicating the string contains only alphabetic characters.

Example : In this example we defines a function isAlphabetic to check if a string consists only of alphabetic characters using the isNaN function. It tests the function with two strings and prints the results.

JavaScript
let str1 = "w3wiki";
let str2 = "Beginner123";

function isAlphabetic(str) {
    return [...str].every(char => isNaN(char));
}

console.log(isAlphabetic(str1)); 
console.log(isAlphabetic(str2)); 

Output
true
false

Using Array.prototype.filter and String.prototype.match

In this approach, we convert the string into an array of characters using the split method. We then use the filter method to keep only the alphabetic characters by matching each character against a regular expression. If the length of the filtered array is equal to the length of the original string, it means the string contains only alphabetic characters.

Example: In this example, the function ‘containsOnlyAlphabets’ filters the string to remove any non-alphabetic characters and compares the length of the filtered string with the original string.

JavaScript
function containsOnlyAlphabets(str) {
    const filteredArray = str.split('').filter(char => char.match(/^[a-zA-Z]$/));
    return filteredArray.length === str.length;
}

let str1 = "w3wiki";
let str2 = "Beginner123";

console.log(containsOnlyAlphabets(str1)); // true
console.log(containsOnlyAlphabets(str2)); // false

Output
true
false

Using Array.prototype.reduce

In this method, we will create a function that uses the reduce method to check if all characters in the string are alphabetic. The reduce method will iterate through each character, checking if it is within the range of alphabetic characters.

Example: In this example, we will use the reduce method to check each character in the string.

JavaScript
function containsOnlyAlphabets(str) {
    return str.split('').reduce((acc, char) => {
        return acc && (char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z');
    }, true);
}

const str1 = "w3wiki";
const str2 = "Beginner123";

console.log(containsOnlyAlphabets(str1)); // true
console.log(containsOnlyAlphabets(str2)); // false
// Nikunj Sonigara

Output
true
false