Check if String contains only Letters in JavaScript-Code Examples

Radu TM

Check if String contains only Letters in JavaScript

JavaScript by Radu TM June 22, 2022

function checkString(str) {
  // we use a regex here to check if the string contains only letters
  const regex = /^[a-zA-Z]+$/;
  // if the regex returns true, then we know the string contains only letters
  if (regex.test(str)) {
    return true;
  }
  // if the regex returns false, then we know the string does not contain only letters
  return false;
}

// let's test our function
const str1 = "hello"; // this should return true
const str2 = "hello world"; // this should return false because it contains a space
const str3 = ""; // this should return false because it's an empty string

console.log(checkString(str1)); // true
console.log(checkString(str2)); // false
console.log(checkString(str3)); // false

0

23.658

Check if String contains only Letters in JavaScript

JavaScript by Radu TM June 22, 2022

function checkLetters(str) {
  // we use a regular expression to check if the string contains only letters
  const regex = /^[a-zA-Z]+$/;
  // if the regex doesn't match, then we know the string contains non-letter characters
  if (!regex.test(str)) {
    return false;
  }
  // otherwise, the string contains only letters
  return true;
}

// tests
console.log(checkLetters('Hello world!')); // false
console.log(checkLetters('Hello')); // true

0

23.658

Check if String contains only Letters and Numbers in JS

JavaScript by Radu TM June 22, 2022

// This function will check if a string contains only letters and numbers.

function checkString(str) {
  // Use a regular expression to remove all non-letter and non-number characters.
  str = str.replace(/[^A-Za-z0-9]/g, '');

  // If the string is empty after removing all non-letter and non-number
  // characters, then it contains only letters and numbers.
  if (str.length === 0) {
    return true;
  }

  // Otherwise, the string contains at least one character that is not a letter
  // or number.
  return false;
}

// Test the checkString function.

console.log(checkString('abc123')); // true
console.log(checkString('[email protected]#')); // false
console.log(checkString('123')); // true
console.log(checkString('')); // true
console.log(checkString('[email protected]#')); // false

0

10.761

Check if String contains only Letters and Spaces in JS

JavaScript by Radu TM June 22, 2022

function isStringOnlyLettersAndSpaces(str) {
  // We use a Regular Expression to check if the string only contains letters and spaces
  // The "i" at the end of the expression makes it case insensitive
  var regex = /^[a-z\s]+$/i;

  // We return true if the string passes the regex test
  return regex.test(str);
}

// Test the function
console.log(isStringOnlyLettersAndSpaces("I love JS")); // true
console.log(isStringOnlyLettersAndSpaces("I #JS")); // false

0

10.761

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories