How would I write a JavaScript function that detects if the user is typing in all-caps in a <textarea>
, and disallow submitting of the form until the offender has corrected their text? Understandably, a definition of all-caps is up for some debate, but here's what I'm aiming to achieve:
- YOU CAN'T DO THAT. IT'S NOT SMART. is obviously what I'm trying to snake out, because it's obnoxious.
- I like this this school. is acceptable because single letter "all capital" words are a part of the English language.
- I mean, COME ON! This is ridiculous! should this be acceptable? In a text field that doesn't support rich text, a user needs to be able to express emphasism. I think this should be acceptable.
Anyone have any suggestions? I imagine a regex is in order.
How would I write a JavaScript function that detects if the user is typing in all-caps in a <textarea>
, and disallow submitting of the form until the offender has corrected their text? Understandably, a definition of all-caps is up for some debate, but here's what I'm aiming to achieve:
- YOU CAN'T DO THAT. IT'S NOT SMART. is obviously what I'm trying to snake out, because it's obnoxious.
- I like this this school. is acceptable because single letter "all capital" words are a part of the English language.
- I mean, COME ON! This is ridiculous! should this be acceptable? In a text field that doesn't support rich text, a user needs to be able to express emphasism. I think this should be acceptable.
Anyone have any suggestions? I imagine a regex is in order.
Share Improve this question asked May 26, 2013 at 3:17 Jody HeavenerJody Heavener 2,8745 gold badges45 silver badges72 bronze badges 9- Probably you got to look for ASCII chars > 95. – Abilash Commented May 26, 2013 at 3:18
- 3 Maybe just set a threshold for the percent of uppercase characters permitted? No more than 50%? I think the answer won't be hard once you specify the problem more precisely. – Dave S. Commented May 26, 2013 at 3:21
- Your definition still isn't exact enough. How would you measure that? At most n words in caps allowed? – bfavaretto Commented May 26, 2013 at 3:22
- I'm not sure defining a threshold is appropriate, because the user could essentially type every second word in all caps and maintain the under 50% threshold, no? I think there needs to be some sort of logic applied – Jody Heavener Commented May 26, 2013 at 3:24
- 1 If the text area contains NO lower-case letters, and it contains ANY upper-case letters, then it's all-caps. The second check avoids plaining if the field is just numbers (although that seems unlikely for a textarea. – Barmar Commented May 26, 2013 at 3:33
3 Answers
Reset to default 6Javascript function for checking that a string has uppercase letters but no lowercase letters:
function allCaps(word) {
var containsUpper = /[A-Z]/.test(word);
var containsLower = /[a-z]/.test(word);
return containsUpper && !containsLower;
}
I suggest getting a ratio of uppercased letters. Here is a way:
function getCapsRatio(val) {
// Get the number of uppercase letters
var up = (val.match(/[A-Z]/g) || '').length,
// Get the number of letters (no space)
fullLetters = (val.match(/[^\s+]/g) || '').length;
// So the ratio of uppercase letters pared to downcase is...
return (up * 100) / fullLetters;
}
So you'd use this function like this:
var ratio = getCapsRatio(value);
if (ratio > 50) {
// 50% of uppercase letters? this guy is all caps!
}
This should be studied, but I guess 50% is a good ratio to think the guy is all caps.
Perhaps simply this this :
/[a-z]/i.test(str) && str.toUpperCase () == str
Compare the original string with itself up-cased, if equal then user has typed only upper-case letters.