Open In App

JavaScript Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values

Last Updated : 17 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how can we check if a string contains uppercase, lowercase, special characters, and numeric values. We have given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and numeric values or not. If the string contains all of them, then returns “true”. Otherwise, return “false”.

Examples:

Input : str = "GeeksforGeeks@123"
Output : trueput: Yes 
Explanation: The given string contains uppercase, lowercase, 
special characters, and numeric values.
Input : str = “GeeksforGeeks” 
Output : No 
Explanation: The given string contains only uppercase 
and lowercase characters.

Using Regular Expression

We can use regular expressions to solve this problem. Create a regular expression to check if the given string contains uppercase, lowercase, special character, and numeric values as mentioned below:

Syntax:

regex = “^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)” + “(?=.*[-+_!@#$%^&*., ?]).+$” 

where,

  • ^ represents the starting of the string.
  • (?=.*[a-z]) represent at least one lowercase character.
  • (?=.*[A-Z]) represents at least one uppercase character.
  • (?=.*\\d) represents at least one numeric value.
  • (?=.*[-+_!@#$%^&*., ?]) represents at least one special character.
  • . represents any character except line break.
  • + represents one or more times.

Match the given string with the Regular Expression using Pattern.matcher(). Print True if the string matches with the given regular expression. Otherwise, print False.

Example:This example shows the use of the above-explained approach.

Javascript
// JavaScript Program to Check if a string contains
// uppercase, lowercase, special characters and
// numeric values
function isAllCharPresent(str) {
    let pattern = new RegExp(
        "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$"
    );

    if (pattern.test(str))
        return true;
    else
        return false;
    return;
}

// Driver Code
const str = "#GeeksForGeeks123@";

console.log(isAllCharPresent(str));

Output
true

Using Array Methods

Convert the string to an array of characters. Utilize array methods like some() to check for uppercase, lowercase, special characters, and isNaN() to check for numeric values. Return the results in an object.

Example: In this example The function checkString analyzes a string for uppercase, lowercase, numeric, and special characters, returning an object indicating their presence. It’s then tested with a sample string.

JavaScript
function checkString(str) {
  const chars = Array.from(str);

  const hasUppercase = chars.some(char => /[A-Z]/.test(char));
  const hasLowercase = chars.some(char => /[a-z]/.test(char));
  const hasNumeric = chars.some(char => !isNaN(char) && char !== ' ');
  const hasSpecial = chars.some(char => /[!@#$%^&*(),.?":{}|<>]/.test(char));

  return {
    hasUppercase,
    hasLowercase,
    hasNumeric,
    hasSpecial
  };
}

const result = checkString("HelloWorld123!");
console.log(result);

Output
{
  hasUppercase: true,
  hasLowercase: true,
  hasNumeric: true,
  hasSpecial: true
}



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads