Forum Moderators: open
I'm too in the upper case war.
I'm looking a snippet of code that allows me to detect a percentage of upper case text and do not allow to publish anything beyond this fixed percentage.
The idea I have is to build some code that, once the handler onblur or onsubmit happens, take a look in the input (or textarea) field and, doing some maths, detects too much upper case text, stops the submission and shows a messsage.
What I saw doing some research is a lot of code related to caps lock in form an password input fields. This kind of code does not prevent to those copypasters who goes from a classified ads service to another one from sunrise to sunset.
My worst problem is that I can understand how javascript works but I'm absolutely unable to write a single line of valid code.
Can anyone help me?
I feel like this is what you are looking for, is it not? Does the issue lie with the fact that you can only have a fixed percentage? Since the text size could be more or less depending on what is typed, you are probably going to want to use some sort of percentage-based system to calculate whether or not there are too many capital letters in the string.
Also note that adding a javascript validation does not mean that it will "detect" all cases because javascript can be turned off in the browsers not, or there just may be a browser that doesn't support javascript altogether. You should really be doing this validation server-side if you want to be sure that it will not contain a certain amount of uppercase letters.
Probably this is the long way to do it but maybe it will work:
- Once the onblur happens, the script counts the letters
- Then check every letter and detects if it's upper or lower case
- Having the total numbers, applies the percentage to detect if the text has too much upper case (let's accept that a normal text has a 10% of upper case or less)
- if the percentage of upper case is exceeded, the script stops submission and shows a message
[pre]
function countCaps() {
var text = document.getElementById("text_holder").value;
var count = 0;
for(i=0; i < text.length; i++) {
if(text[i].charCodeAt(text[i]) >= 65 && text[i].charCodeAt(text[i]) <= 90)
count++;
}
alert("Number: " + count);
alert("Percentage: " + count/text.length*100 + " %");
}
[/pre]
You could also use regular expressions for the same affect
function countCaps() {
var matchList = "";
var regexp = /[A-Z]/g;
var text = document.getElementById("text_holder").value;
while ((match = regexp.exec(text)) != null) {
matchList += val;
}
alert("Number: " + matchList.length);
alert("Percentage: " + matchList.length/text.length * 100 + "%");
}
I got it formatted but it was somewhat difficult.
The only I need now is to know/understand what should I got to make the script stops the submission task until the text is changed to lower case.
There is something I must add to the form? Is something I'm missing in the function?
function countCaps()
{
val = 1;
var matchList = "1";
var regexp = /[A-Z]/g;
var text = document.getElementById("adtitle").value;
var myMsg='El texto contiene demasiadas mayusculas. Por favor use minusculas en sus textos.\n\n Gracias.';
while ((match = regexp.exec(text)) != null) {
matchList += val;
}
alert("Number: " + matchList.length);
alert("Percentage: " + matchList.length/text.length * 100 + "%");
var percentage = matchList.length/text.length * 100 ;
if ( percentage >= 30 ) {
alert( myMsg );
}
}
This is the form where the script should work on:
______________________
<form action="index.php" method="post" name="frmPost" onsubmit="countCaps()">
<input name="formtitle" type="text" id="adtitle" size="80" maxlength="100" value="">
<button type="submit">Send</button>
______________________
function countCaps()
{
val = 1;
var matchList = "1";
var regexp = /[A-Z]/g;
var text = document.getElementById("adtitle").value;
var myMsg='El texto contiene demasiadas mayusculas. Por favor use minusculas en sus textos.\n\n Gracias.';
while ((match = regexp.exec(text)) != null) {
matchList += val;
}
alert("Number: " + matchList.length);
alert("Percentage: " + matchList.length/text.length * 100 + "%");
var percentage = matchList.length/text.length * 100 ;
if ( percentage >= 30 ) {
alert( myMsg );
return false;
}
return true;
}
______________________
<form action="index.php" method="post" name="frmPost" onsubmit="return countCaps()">
<input name="formtitle" type="text" id="adtitle" size="80" maxlength="100" value="">
<button type="submit">Send</button>
</form>
This is a problem because you are returning the wrong numbers of capitals. What the function does is go through your test and pull out each of the capitals and store it in the variable.
Given:
var matchList = "";
var myMsg='El texto contiene demasiadas mayusculas. Por favor use minusculas en sus textos.\n\n Gracias.';
alert(matchList) would display "EPG". Length = 3.
In your code:
var matchList = "1";
var myMsg='El texto contiene demasiadas mayusculas. Por favor use minusculas en sus textos.\n\n Gracias.';
alert(matchList) would display "1EPG". Length = 4.
_________________________________________________
Note: RegExp are powerful using the one line you can easily change the function to mean something totally different.
For instance instead of only counting capitals throughout the text, you might want to count words with capitals; or even, words greater than one letter long; or maybe, only capitals inside of words, not including the first letter of the word. Then you could use another RegExp to count the number of words total.
So,
(# of capitals not including the first letter of the word)/(# of words total) * 100 = a more realistic %
/\B[A-Z]/g[/3]
function countCaps()
{
var regexp = /[A-Z]/g;
var text = document.getElementById("adtitle").value;
var myMsg='El texto contiene demasiadas mayusculas. Por favor use minusculas en sus textos.\n\n Gracias.';
var matchList = text.match(regexp);
alert("Number: " + matchList.length);
alert("Percentage: " + matchList.length/text.length * 100 + "%");
var percentage = matchList.length/text.length * 100 ;
if ( percentage >= 30 ) {
alert( myMsg );
return false;
}
return true;
}