Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - checking for any lowercase letters in a string

Consider a JavaScript method that needs to check whether a given string is in all uppercase letters. The input strings are people's names.

The current algorithm is to check for any lowercase letters.

var check1 = "Jack Spratt";    
var check2 = "BARBARA FOO-BAR"; 
var check3 = "JASON D'WIDGET";  

var isUpper1 = HasLowercaseCharacters(check1);  
var isUpper2 = HasLowercaseCharacters(check2);
var isUpper3 = HasLowercaseCharacters(check3);

function HasLowercaseCharacters(string input)
{
    //pattern for finding whether any lowercase alpha characters exist
    var allLowercase; 

    return allLowercase.test(input);
}

Is a regex the best way to go here?

What pattern would you use to determine whether a string has any lower case alpha characters?

like image 358
p.campbell Avatar asked May 13 '10 22:05

p.campbell


People also ask

How do you check if all letters in a string are lowercase?

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

How do you check if a string is all lowercase JavaScript?

log(str. toUpperCase() === str. toLowerCase()); If we know that the string is uppercase and it's not equal to it's lowercase variant, then we have an all uppercase string.

How do you check if all letters in a string are capitalized?

You should use str. isupper() and str. isalpha() function.

Which method is used to lowercase all the characters in a string in JavaScript?

The toLowerCase() method returns the value of the string converted to lower case. toLowerCase() does not affect the value of the string str itself.


2 Answers

function hasLowerCase(str) {     return str.toUpperCase() != str; }  console.log("HeLLO: ", hasLowerCase("HeLLO")); console.log("HELLO: ", hasLowerCase("HELLO"));
like image 115
karim79 Avatar answered Sep 16 '22 11:09

karim79


also:

function hasLowerCase(str) {
    return (/[a-z]/.test(str));
}
like image 45
ariel Avatar answered Sep 17 '22 11:09

ariel