How to use Array.prototype.every In Javascript
In this approach, we will create a function that uses the every method to check if all characters in the string meet certain criteria: uppercase, lowercase, numeric, and special characters.
Example: In this example, we will use the every method to check each character in the string.
function isAllCharPresent(str) {
const hasUppercase = [...str].some(char => /[A-Z]/.test(char));
const hasLowercase = [...str].some(char => /[a-z]/.test(char));
const hasNumeric = [...str].some(char => /\d/.test(char));
const hasSpecial = [...str].some(char => /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(char));
return hasUppercase && hasLowercase && hasNumeric && hasSpecial;
}
const str = "#w3wiki123@";
console.log(isAllCharPresent(str)); // Output: true
const str2 = "w3wiki";
console.log(isAllCharPresent(str2)); // Output: false
// Nikunj Sonigara
Output
true false
JavaScript Program to Check if a String Contains Uppercase, Lowercase, Special Characters and Numeric Values
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 = "w3wiki@123"
Output : trueput: Yes
Explanation: The given string contains uppercase, lowercase,
special characters, and numeric values.
Input : str = “w3wiki”
Output : No
Explanation: The given string contains only uppercase
and lowercase characters.
Table of Content
- Using Regular Expression
- Using Array Methods
- Using a Frequency Counter Object:
- Using Array.prototype.every
Contact Us