• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if String Contains Only Letters

Using JavaScript to Check if String Contains Only Letters

September 13, 2022 Leave a Comment

We can use JavaScript to check if a string contains only letters. One of the simplest ways to do this is to use the JavaScript test() method and a regex expression.

/^[a-zA-Z]+$/.test(someString);

The regex expression [a-zA-Z] will check for any letter lowercase or upper. The rest of the regex expression checks that every character in the string is equal to a letter. Here is a useful resource on regex expressions if you want to learn more about them. The test() method will check the string you pass as a parameter to see if only letters are contained in the string due to the regex expression. If it does find all letters, it returns true. Otherwise, it returns false.

We can wrap this code in a function to make it easy to reuse.

function stringContainsOnlyLetters(str){
  return /^[a-zA-Z]+$/.test(str);
};

Now let’s test this function with a couple of examples:

function stringContainsOnlyLetters(str){
  return /^[a-zA-Z]+$/.test(str);
};

var string1 = "This is a string with letters and spaces";
var string2 = "Thisisastringwithonlyletters";
var string3 = "Hello";
var string4 = "hello";
var string5 = "This is a string with letters and spaces and a period.";


console.log(stringContainsOnlyLetters(string1));
console.log(stringContainsOnlyLetters(string2));
console.log(stringContainsOnlyLetters(string3));
console.log(stringContainsOnlyLetters(string4));
console.log(stringContainsOnlyLetters(string5));

#Output
false
true
true
true
false

If we wanted to make all of these examples return true by allowing spaces and periods, we would simply need to our regular expression.

function stringContainsLettersSpacesPeriods(str){
  return /^[a-zA-Z .]+$/.test(str);
};

var string1 = "This is a string with letters and spaces";
var string2 = "Thisisastringwithonlyletters";
var string3 = "Hello";
var string4 = "hello";
var string5 = "This is a string with letters and spaces and a period.";


console.log(stringContainsLettersSpacesPeriods(string1));
console.log(stringContainsLettersSpacesPeriods(string2));
console.log(stringContainsLettersSpacesPeriods(string3));
console.log(stringContainsLettersSpacesPeriods(string4));
console.log(stringContainsLettersSpacesPeriods(string5));

#Output
true
true
true
true
true

Hopefully this article has been useful for you to learn how to use JavaScript to check if a string contains only letters.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get the Last Day of the Month
  • 2.  Check if Character is Uppercase in JavaScript
  • 3.  Using JavaScript to Reverse an Array
  • 4.  JavaScript offsetTop – Get the Top Position of Element
  • 5.  Using JavaScript to Change the Font Size of a Paragraph
  • 6.  How to Create a New h1 Element With JavaScript
  • 7.  Using JavaScript to Replace a Space with an Underscore
  • 8.  How to Convert Radians to Degrees Using JavaScript
  • 9.  Convert String to Float in JavaScript
  • 10.  Using JavaScript to Check if Variable is a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy