Warning: session_start(): open(/tmp/sess_7439c77b7b9d26fbc6d0f51423bd4b59, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Check If String Contains Only Letters And Spaces In JavaScript Easily - LearnShareIT

Check If String Contains Only Letters And Spaces In JavaScript Easily

Hi guys, to continue the topic of checking a String in JavaScript, today we will discuss about how to check if String contains only Letters and Spaces in JavaScript. Scroll down to get started.

Check if String contains only Letters and Spaces in JavaScript

Below are some simple ways we want to introduce to you. These ways work well, even on old browser versions.

Using the RegExp.prototype.test() method

We have introduced the syntax of the RegExp.prototype.test() method here. You can read it if you want.

In this case, we will use the regex string: /^[a-z\s]+$/i

In there:

  • ^: represents the starting point of the regex.
  • [ ]: matches the characters inside it.
  • a-z: corresponds to the letters from a to z.
  • \s: corresponds to a space.
  • +: appears more than or equal to 1 time.
  • $: represents the endpoint of the regex.
  • i: enables case-insensitive.

First, we call test() on the regex above and perform matching in the given String. Then, if the given String contains only spaces and letters, test() returns true. If not, test() returns false.

const url = 'learnshareit.com';
const goal = 'learn and share IT knowledge';

function checkLettersAndSpaces(string) {
  const regex = /^[a-z\s]+$/i;

  // Using test()
  const isLettersAndSpaces = regex.test(string); // True or False

  if (isLettersAndSpaces) {
    console.log(`'${string}' contains only Letters and Spaces.`);
    return true;
  } else {
    console.log(`${string} does not contain only Letters and Spaces.`);
    return false;
  }
}

checkLettersAndSpaces(goal); // true
checkLettersAndSpaces(url); // false

Output:

'learn and share IT knowledge' contains only Letters and Spaces.
'learnshareit.com' does not contain only Letters and Spaces.

Using the String.prototype.search() method

Before reading on, you can read the String.prototype.search() syntax here.

In this way, we still use the regex string like the one above, but this time we will pass that regex string into search(). If it returns a number other than -1, our String exists only letters and spaces. If it returns -1, our String exists characters other than letters and spaces.

const url = 'learnshareit.com';
const goal = 'learn and share IT knowledge';

function checkLettersAndSpaces(string) {
  const regex = /^[a-z\s]+$/i;

  // Using search()
  const index = string.search(regex); // index = -1 or another number

  if (index !== -1) {
    console.log(index); // A number except -1
    console.log(`'${string}' contains only Letters and Spaces.`);
    return true;
  } else {
    console.log(index); // -1
    console.log(`'${string}' does not contain only Letters and Spaces.`);
    return false;
  }
}

checkLettersAndSpaces(goal); // true
checkLettersAndSpaces(url); // false

Output:

0
'learn and share IT knowledge' contains only Letters and Spaces.
-1
'learnshareit.com' does not contain only Letters and Spaces.

Using the String.prototype.match() method

We introduced String.prototype.match() in this article. You can learn more if you want.

To use match(), we must pass the regular expression as an argument. Every time the regex string is found in the given String, it will be pushed into the array. In the end, the return is an array. If there is no match, the return is null.

const url = 'learnshareit.com';
const goal = 'learn and share IT knowledge';

function checkLettersAndSpaces(string) {
  const regex = /^[a-z\s]+$/i;

  // Using match()
  const matchArr = string.match(regex); // matchArr = An Array of matches or null

  if (matchArr !== null) {
    console.log(matchArr); // An Array of matches
    console.log(`'${string}' contains only Letters and Spaces.`);
    return true;
  } else {
    console.log(matchArr); // null
    console.log(`'${string}' does not contain only Letters and Spaces.`);
    return false;
  }
}

checkLettersAndSpaces(goal); // true
checkLettersAndSpaces(url); // false

Output:

["learn and share IT knowledge"]
'learn and share IT knowledge' contains only Letters and Spaces.
null
'learnshareit.com' does not contain only Letters and Spaces.

Summary

Above, we have introduced some ways to check if String contains only Letters and Spaces in JavaScript. If you have a better way, please share it with everyone.

Have a lucky day!

Leave a Reply

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