Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a string is all uppercase in JavaScript? [duplicate]

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

example of matching:

"hello" => false "Hello" => false "HELLO" => true 
like image 922
Danilo Avatar asked Jul 10 '13 14:07

Danilo


People also ask

How can I check if a string is all uppercase in Javascript?

To check if a string is all uppercase, use the toUppercase() method to convert the string to uppercase and compare it to itself. If the comparison returns true , then the string is all uppercase.

How do you check if all strings are uppercase?

To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.

Which method returns a copy of all uppercase string?

upper() Return Value upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.

Is upper case in Javascript?

The toUpperCase() method converts a string to uppercase letters. The toUpperCase() method does not change the original string.


1 Answers

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 
like image 100
Andrew Whitaker Avatar answered Oct 05 '22 13:10

Andrew Whitaker