Skip to content
the poor coder

Check if input is a letter

Poor Man
Published
Reading time
2 min read
On this page

Check if input is a letter#

As a blogger, I have come across a lot of instances where I needed to check if a user input is a letter or not. The following are some ways you can achieve this in HTML:

Using Regular Expression#

You can use regular expressions to check if the input is a letter or not. You can define a regular expression pattern which checks for any character from A to Z, both uppercase and lowercase.

JavaScript

      function isLetter(input) {
        const pattern = /^[a-zA-Z]+$/;
        return pattern.test(input);
      }
    

The above code defines a function called isLetter which takes an input parameter and returns true if the input consists of only letters and false otherwise. The regular expression pattern /^[a-zA-Z]+$/ checks if the input contains only uppercase or lowercase letters.

Using ASCII values#

You can also use the ASCII values of the characters to check if the input is a letter or not. Every character in JavaScript has an associated ASCII value. Uppercase letters start from 65 and end at 90, while lowercase letters start from 97 and end at 122. Any character outside this range is not a letter.

JavaScript

      function isLetter(input) {
        const code = input.charCodeAt(0);
        return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
      }
    

The above code defines a function called isLetter which takes an input parameter and returns true if the input consists of only letters and false otherwise. The charCodeAt() method is used to get the ASCII value of the first character in the input string.

Using isNaN() function#

You can also use the isNaN() function to check if the input is a letter or not. The isNaN() function returns true if the input is not a number, and false otherwise. Since letters are not numbers, this function can be used to check if the input is a letter or not.

JavaScript

      function isLetter(input) {
        return isNaN(input);
      }
    

The above code defines a function called isLetter which takes an input parameter and returns true if the input consists of only letters and false otherwise. The isNaN() function is used to check if the input is a number or not. Since letters are not numbers, this function can be used to check if the input is a letter or not.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC