Warning: session_start(): open(/tmp/sess_32c757242c1a8d10fc31b0c7d4cef6b5, 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 Numbers In JavaScript - LearnShareIT

Check If String Contains Only Letters And Numbers In JavaScript

In the previous post, we showed you 3 ways to check whether the String contains only alphabetic characters. Today we will show you solutions for how to check if String contains only Letters and Numbers in JavaScript. Read on to learn more.

Check if String contains only Letters and Numbers in JavaScript

Like the previous article, we will also show you 3 simple ways this time.

Using test() method

You can read more syntax of test() here.

We will use the regex string: /^[A-Za-z0-9]+$/ to call test() and execute the test on the given String. Test() will return true if String exists nothing except alphanumeric characters. Otherwise, if there is a non-alphanumeric character, it will return false.

Let us explain the regex string: /^[A-Za-z0-9]+$/ to you.

  • ^: marks the start of the regex string.
  • [ ]: matches the characters inside it.
  • +: occur 1 or more times.
  • A-Z: represents the letters from A to Z.
  • a-z: represents the letters from a to z.
  • 0-9: represent digits from 0 to 9.
  • $: marks the end of the regex string.

See the example below for how to use test():

const account = 'btwr2022';
const email = 'btwr@learnshareit.com';

function checkLettersAndNumbers(str) {

  // Use test() to check the str
  let isAlphanumeric = /^[a-zA-Z0-9]+$/.test(str);

  if (isAlphanumeric) {
    console.log(`${str} contains only Letters and Numbers!`);
    return true;
  } else {
    console.log(`${str} does not contain only Letters and Numbers!`);
    return false;
  }

}

checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false

Output:

btwr2022 contains only Letters and Numbers!
btwr@learnshareit.com does not contain only Letters and Numbers!

Using search() method

The search() method will search for a match of the regex string in the given String and return the position of the first match in the given String. If it returns -1, it means no value matches the regex string.

You can learn the syntax of search() here.

First, we will pass the regex mentioned above into the search() method to perform the search.

Next, we use the if else statement to check the return. If the return is -1, then our String contains non-alphanumeric characters. In contrast, our String exists nothing except alphanumeric characters.

const account = 'btwr2022';
const email = 'btwr@learnshareit.com';

function checkLettersAndNumbers(str) {

  // Use search() to check the str
  let position = str.search(/^[a-zA-Z0-9]+$/);

  if (position === -1) {

    // Position = -1 -> A non-alphanumeric character exists
    console.log(position); // -1

    console.log(`${str} does not contain only Letters and Numbers!`);
    return false;
  } else {

    // Position != -1 -> Contains only letters and numbers
    console.log(position); // 0

    console.log(`${str} contains only Letters and Numbers!`);
    return true;
  }

}

checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false

Output:

0
btwr2022 contains only Letters and Numbers!
-1
btwr@learnshareit.com does not contain only Letters and Numbers!

Using match() method

Before reading on, you can read more about the match() method syntax here.

Similar to the search() method, we have to pass the regex: /^[a-zA-Z0-9]+$/ to the match() method to start matching. The match() method will return null if there are no matches in the String or return an array containing the characters that match the regex string.

const account = 'btwr2022';
const email = 'btwr@learnshareit.com';

function checkLettersAndNumbers(str) {

  // Use match() to check the str
  const matches = str.match(/^[a-zA-Z0-9]+$/);

  if (matches === null) {

    // Matches = null -> A non-alphanumeric character exists
    console.log(matches); // null

    console.log(`${str} does not contain only Letters and Numbers!`);
    return false;
  } else {

    // Matches != null -> Contains only letters and numbers
    console.log(matches); // An Array

    console.log(`${str} contains only Letters and Numbers!`);
    return true;
  }

}

checkLettersAndNumbers(account); // true
checkLettersAndNumbers(email); // false

Output:

["btwr2022"]
btwr2022 contains only Letters and Numbers!
null
btwr@learnshareit.com does not contain only Letters and Numbers!

Summary

Through this article about how to check if String contains only Letters and Numbers in JavaScript, we hope to make it easier for you to work with String. If you have any questions, please leave a comment below. Thank you for reading!

Leave a Reply

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