49

There is a Javascript/Jquery boolean function to test if a string is all uppercase?

example of matching:

"hello" => false
"Hello" => false
"HELLO" => true
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
Danilo
  • 2,016
  • 4
  • 24
  • 49
  • 6
    If I google for `"How can I check if a string is all uppercase in JavaScript? [duplicate]"` I find this question – Bitterblue Jul 09 '15 at 11:36
  • 3
    Same, this is the top result for testing if an entire string is uppercase. Furthermore testing an entire string is a bit different than checking a specific character. – AlbertEngelB Oct 14 '16 at 14:35

5 Answers5

112
function isUpperCase(str) {
    return str === str.toUpperCase();
}


isUpperCase("hello"); // false
isUpperCase("Hello"); // false
isUpperCase("HELLO"); // true

You could also augment String.prototype:

String.prototype.isUpperCase = function() {
    return this.valueOf().toUpperCase() === this.valueOf();
};


"Hello".isUpperCase(); // false
"HELLO".isUpperCase(); // true
Mike S
  • 1,537
  • 2
  • 11
  • 15
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • 21
    `isUpperCase("123"); // true` – Derek Prior Oct 16 '14 at 14:57
  • To reiterate Derek's comment... a string consisting of only numbers will return true, incorrectly indicating all upper case. – rg88 Apr 19 '15 at 20:22
  • @Stuart Why incorrectly ? I guess the problem here is that a string with no characters possessing the uppercase-capability is not allowed on the true side of is-uppercase ? – Bitterblue Jul 09 '15 at 11:23
  • @Bitterblue Basically, that's my understanding. There should be all uppercase characters and numbers are neither. – rg88 Jul 09 '15 at 12:34
  • @Stuart I'm not arguing, just saying. What if you need to know if a member is a constant and not a variable? And all you have is the name: `myVariable`, `MY_CONSTANT`, `MY_CONSTANT_123`. – Bitterblue Jul 09 '15 at 14:56
  • @Bitterblue I don't think there is an easy way to resolve that short of adopting a policy of "no numbers in constants". I suppose you could create a function that would rely on the ES6 const so that when you attempt to change a value, if it is a constant, it would throw an error. This might not work in certain browsers though. Easier to adopt a naming policy. – rg88 Jul 12 '15 at 14:09
  • `str.toUpperCase() === str && /\p{Lu}/u.test(str)` also checks if `str` contains an uppercase letter that has a lowercase variant avoiding the problem with strings consisting of numbers only – albertamg Apr 25 '19 at 11:52
  • isUpperCase("GAUß"); // logically false, visually true - because the character 'ß' is classified as lowercase - it didn't have an official uppercased version until 2017. Older data-sets might have this issue. Fix : `str.replace("ß","SS") === str.toUpperCase()` – Wex Mar 24 '20 at 13:56
  • Please Note: empty string, numbers and symbols will validate as true. use regex `/^[A-Z]*$/` if you want to check for only uppercase. still +1 – Bobby Axe Jun 30 '20 at 13:32
35

I must write at least one sentence here because they don't like short answers here, but this is the simplest solution I can think of:

s.toUpperCase() === s
valdeci
  • 13,962
  • 6
  • 55
  • 80
16

Here's another option:

function isUpperCase(str) {
  return (/^[^a-z]*$/).test(str);
}
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 10
    Nice, but by changing the regex to `/^[A-Z]*$/` it will discard numbers and punctuations too. – Konamiman Jun 24 '15 at 14:12
  • 3
    Be cautious of using the above regex as it rejects strings like "ABC123" which is clearly uppercase. Instead, use this regex: `function isUpper(str) { return !/[a-z]/.test(str) && /[A-Z]/.test(str) }`. Strings like "123" or "WE23a" returns false while strings like "W390" returns true. – AlienKevin Sep 11 '19 at 21:25
  • @AlienKevin Are you referring to the regex in this answer? It does not reject the string "ABC123"; it rejects any string that contains any lowercase letters. – Dan Tao Sep 16 '19 at 10:04
  • 1
    @DanTao Sorry, my mistake. Your regex does not reject "ABC123" but it does accept "123" which is not uppercase. That's why I proposed a more robust version. – AlienKevin Sep 20 '19 at 16:10
4

Just use:

if(mystring === mystring.toUpperCase())
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1
var test = "HELLO";
var upper = test.toUpperCase();

return test === upper; // true

// other example

var test = "Hello";
var upper = test.toUpperCase();

return test === upper; // false
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221