How to Test If a Letter in a String Is Uppercase or Lowercase Using JavaScript

How can I test if a letter in a string is uppercase or lowercase using JavaScript?

The answer by josh and maleki will return true on both upper and lower case if the character or the whole string is numeric. making the result a false result.
example using josh

var character = '5';
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}

another way is to test it first if it is numeric, else test it if upper or lower case
example

var strings = 'this iS a TeSt 523 Now!';
var i=0;
var character='';
while (i <= strings.length){
character = strings.charAt(i);
if (!isNaN(character * 1)){
alert('character is numeric');
}else{
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
}
i++;
}

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

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

How can I test if a letter in a string is uppercase or lowercase using JavaScript?

The answer by josh and maleki will return true on both upper and lower case if the character or the whole string is numeric. making the result a false result.
example using josh

var character = '5';
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}

another way is to test it first if it is numeric, else test it if upper or lower case
example

var strings = 'this iS a TeSt 523 Now!';
var i=0;
var character='';
while (i <= strings.length){
character = strings.charAt(i);
if (!isNaN(character * 1)){
alert('character is numeric');
}else{
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
}
i++;
}

How could I detect if there is an uppercase character in a string

You can use the same logic you used for all uppercase, turn the string to lowercase and if its not equal to the original string it has an uppercase letter in it.

function hasUpperCase(str) {
return str !== str.toLowerCase();
}

console.log(hasUpperCase("hello")); // false
console.log(hasUpperCase("Hello")); // true
console.log(hasUpperCase("HELLO")); // true

Javascript checking if a letter is an uppercase

You could use a regular expression and look for upper case letters only. Then replace with dash and a lower case letter.

var string = 'borderRadius:0px,fontSize:8px';
console.log(string.replace(/[A-Z]/g, s => '-' + s.toLowerCase()));

Check if user input string contains only uppercase and/or lowercase letters

This ended up working for me:

// only letters are acceptable characters, spaces, special characters, and 
// numbers will throw an error
if (!/^[A-Z]+$/i.test(n_seq)) {
alert("Invalid input!");
return;
}

JavaScript - checking for any lowercase letters in a string

also:

function hasLowerCase(str) {
return (/[a-z]/.test(str));
}


Related Topics



Leave a reply



Submit