How can I 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++;
}
if (character == character.toLowerCase())
{
// The character is lowercase
}
else
{
// The character is uppercase
}
This will log true if character is uppercase letter, and log false in every other case:
var letters = ['a', 'b', 'c', 'A', 'B', 'C', '(', ')', '+', '-', '~', '*'];
for (var i = 0; i<letters.length; i++) {
if (letters[i] === letters[i].toUpperCase()
&& letters[i] !== letters[i].toLowerCase()) {
console.log(letters[i] + ": " + true);
} else {
console.log(letters[i] + ": " + false);
}
}
You may test it here: http://jsfiddle.net/Axfxz/ (use Firebug or sth).
for (var i = 0; i<letters.length; i++) {
if (letters[i] !== letters[i].toUpperCase()
&& letters[i] === letters[i].toLowerCase()) {
console.log(letters[i] + ": " + true);
} else {
console.log(letters[i] + ": " + false);
}
}
and this is for lowercase:).
You could utilize a regular expression test and the toUpperCase method:
String.prototype.charAtIsUpper = function (atpos){
var chr = this.charAt(atpos);
return /[A-Z]|[\u0080-\u024F]/.test(chr) && chr === chr.toUpperCase();
};
// usage (note: character position is zero based)
'hi There'.charAtIsUpper(3); //=> true
'BLUE CURAÇAO'.charAtIsUpper(9); //=> true
'Hello, World!'.charAtIsUpper(5); //=> false
function isUpperCase(myString) {
return (myString == myString.toUpperCase());
}
function isLowerCase(myString) {
return (myString == myString.toLowerCase());
}
More specifically to what is being asked. Pass in a String and a position to check. Very close to Josh's except that this one will compare a larger string. Would have added as a comment but I don't have that ability yet.
function isUpperCase(myString, pos) {
return (myString.charAt(pos) == myString.charAt(pos).toUpperCase());
}
function isLowerCase(myString, pos) {
return (myString.charAt(pos) == myString.charAt(pos).toLowerCase());
}
You can also use a regular expression to explicitly detect uppercase roman alphabetical characters.
isUpperCase = function(char) {
return !!/[A-Z]/.exec(char[0]);
};
EDIT: the above function is correct for ASCII/Basic Latin Unicode, which is probably all you'll ever care about. The following version also support Latin-1 Supplement and Greek and Coptic Unicode blocks... In case you needed that for some reason.
isUpperCase = function(char) {
return !!/[A-ZÀ-ÖØ-ÞΆΈ-ΏΑ-ΫϢϤϦϨϪϬϮϴϷϹϺϽ-Ͽ]/.exec(char[0]);
};
This strategy starts to fall down if you need further support (is Ѭ uppercase?) since some blocks intermix upper and lowercase characters.
I believe this is the easiest solution.. You can use onchange handler in input field .. to do the validation
const isValid = e.target.value === e.target.value.toLowerCase()
if (isValid) {
//Do something
} else {
//Do something
}
function solution(s) {
var c = s[0];
if (c == c.toUpperCase() && !(c >= '0' && c <= '9') &&(c >='A' && c <= 'Z')) {
return 'upper';
} else if (c == c.toLowerCase() && !(c >= '0' && c <= '9') &&(c >='a' && c <= 'z')){
return 'lower';
} else if (c >= '0' && c <= '9'){
return 'digit'
} else {
return 'other'
}
}
var str1= (solution('A')) // upper
var str2 = solution('b') // lower
var str3 = solution('1') // digit
var str4 = solution('_') // other
console.log(`${str1} ${str2} ${str3} ${str4}`)
With modern browsers you can use regexp and unicode property tests e.g.
/\p{Lu}/u.test("A") // is true
/\p{Lu}/u.test("Å") // is true
/\p{Lu}/u.test("a1å") // is false
More info here:
List of general categories here:
function checkCase(c){
var u = c.toUpperCase();
return (c.toLowerCase() === u ? -1 : (c === u ? 1 : 0));
};
Based on Sonic Beard comment to the main answer. I changed the logic in the result:
0: Lowercase
1: Uppercase
-1: neither
Another way is to compare the character with an empty object, i don't really know's why it works, but it works :
for (let i = 1; i <= 26; i++) {
const letter = (i + 9).toString(36).toUpperCase();
console.log('letter', letter, 'is upper', letter<{}); // returns true
}
for (let i = 1; i <= 26; i++) {
const letter = (i + 9).toString(36);
console.log('letter', letter, 'is upper', letter<{}); // returns false
}
so in a function :
function charIsUpper(character) {
return character<{};
}
EDIT: it doesn't work with accents and diacritics, so it's possible to remove it
function charIsUpper(character) {
return character
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')<{};
}
Simply check the ASCII value
// IsLower verify that a string does not contains upper char
func IsLower(str string) bool {
for i := range str {
ascii := int(str[i])
if ascii < 91 && ascii > 64 {
return false
}
}
return true
}
Stephen Nelsons' function converted to a prototype with lots of test examples.
I've also added whole strings to the function for completeness.
See code for additional comments.
/* Please note, there's no requirement to trim any leading or trailing white
spaces. This will remove any digits in the whole string example returning the
correct result. */
String.prototype.isUpperCase = function(arg) {
var re = new RegExp('\\s*\\d+\\s*', 'g');
if (arg.wholeString) {return this.replace(re, '') == this.replace(re, '').toUpperCase()} else
return !!this && this != this.toLocaleLowerCase();
}
console.log('\r\nString.prototype.isUpperCase, whole string examples');
console.log(' DDD is ' + ' DDD'.isUpperCase( { wholeString:true } ));
console.log('9 is ' + '9'.isUpperCase( { wholeString:true } ));
console.log('Aa is ' + 'Aa'.isUpperCase( { wholeString:true } ));
console.log('DDD 9 is ' + 'DDD 9'.isUpperCase( { wholeString:true } ));
console.log('DDD is ' + 'DDD'.isUpperCase( { wholeString:true } ));
console.log('Dll is ' + 'Dll'.isUpperCase( { wholeString:true } ));
console.log('ll is ' + 'll'.isUpperCase( { wholeString:true } ));
console.log('\r\nString.prototype.isUpperCase, non-whole string examples, will only string on a .charAt(n) basis. Defaults to the first character');
console.log(' DDD is ' + ' DDD'.isUpperCase( { wholeString:false } ));
console.log('9 is ' + '9'.isUpperCase( { wholeString:false } ));
console.log('Aa is ' + 'Aa'.isUpperCase( { wholeString:false } ));
console.log('DDD 9 is ' + 'DDD 9'.isUpperCase( { wholeString:false } ));
console.log('DDD is ' + 'DDD'.isUpperCase( { wholeString:false } ));
console.log('Dll is ' + 'Dll'.isUpperCase( { wholeString:false } ));
console.log('ll is ' + 'll'.isUpperCase( { wholeString:false } ));
console.log('\r\nString.prototype.isUpperCase, single character examples');
console.log('BLUE CURAÇAO'.charAt(9) + ' is ' + 'BLUE CURAÇAO'.charAt(9).isUpperCase( { wholeString:false } ));
console.log('9 is ' + '9'.isUpperCase( { wholeString:false } ));
console.log('_ is ' + '_'.isUpperCase( { wholeString:false } ));
console.log('A is ' + 'A'.isUpperCase( { wholeString:false } ));
console.log('d is ' + 'd'.isUpperCase( { wholeString:false } ));
console.log('E is ' + 'E'.isUpperCase( { wholeString:false } ));
console.log('À is ' + 'À'.isUpperCase( { wholeString:false } ));
console.log('É is ' + 'É'.isUpperCase( { wholeString:false } ));
console.log('Ñ is ' + 'Ñ'.isUpperCase( { wholeString:false } ));
console.log('ñ is ' + 'ñ'.isUpperCase( { wholeString:false } ));
console.log('Þ is ' + 'Þ'.isUpperCase( { wholeString:false } ));
console.log('Ͻ is ' + 'Ͻ'.isUpperCase( { wholeString:false } ));
console.log('Ͽ is ' + 'Ͽ'.isUpperCase( { wholeString:false } ));
console.log('Ά is ' + 'Ά'.isUpperCase( { wholeString:false } ));
console.log('Έ is ' + 'Έ'.isUpperCase( { wholeString:false } ));
console.log('ϴ is ' + 'ϴ'.isUpperCase( { wholeString:false } ));
console.log('Ϋ is ' + 'Ϋ'.isUpperCase( { wholeString:false } ));
console.log('Ϣ is ' + 'Ϣ'.isUpperCase( { wholeString:false } ));
console.log('Ϥ is ' + 'Ϥ'.isUpperCase( { wholeString:false } ));
console.log('Ϧ is ' + 'Ϧ'.isUpperCase( { wholeString:false } ));
console.log('Ϩ is ' + 'Ϩ'.isUpperCase( { wholeString:false } ));
console.log('Ϫ is ' + 'Ϫ'.isUpperCase( { wholeString:false } ));
console.log('Ϭ is ' + 'Ϭ'.isUpperCase( { wholeString:false } ));
console.log('Ϯ is ' + 'Ϯ'.isUpperCase( { wholeString:false } ));
console.log('Ϲ is ' + 'Ϲ'.isUpperCase( { wholeString:false } ));
console.log('Ϸ is ' + 'Ϸ'.isUpperCase( { wholeString:false } ));
console.log('Ϻ is ' + 'Ϻ'.isUpperCase( { wholeString:false } ));