Limit Characters and Words Entered
Will Bontrager Nov 17, 2006
General
This script will allow you to limit the number of words and/or characters entered into a form. Easily customizable.
This is an example textarea form field that limits input to either 80 characters or 16 words, whichever comes first.
Notes
The form tag must have a name= attribute. In addition to what's in this example, your form tag will probably have method= and action= attributes, and possibly others.
This example uses a TEXTAREA form field as the text field to be monitored. An INPUT bype="text" field may be used instead.
The next four fields are where the number of characters typed, the number of characters left, the number of words types, and the number of words left are displayed. They all have a readonly attribute so the user can't change the number.
Source Code
Paste this source code into the designated areas.
External file
Paste this code into an external JavaScript file named: charLeft.js
Select code
/* For additional information about this JavaScript
and how to use it, see the "Displaying Number of Words
Typed Into Form Fields" article, linked from the archives
at from http://willmaster.com/possibilities/archives/
The above note and the copyright line must remain with
this JavaScript source code. Comments below this point
in the code may be removed if desired. */
// Customizing this JavaScript code requires specifying eight values.
// Value One:
// Specify the maximum number of characters the form field
// may contain. If you have no maximum, specify 0 (zero).
var MaximumCharacters = "80";
// Value Two:
// Specify the maximum number of words the form field may
// contain. If you have no maximum, specify 0 (zero).
var MaximumWords = "16";
// Value Three:
// Specify the form's name (provided by the name="_____"
// attribute in the FORM tag).
var FormName = "myForm";
// Value Four:
// Specify the name of the text field being monitored
// (provided by the name="_____" attribute in the
// INPUT or TEXTARE tag).
var TextFieldName = "TextField";
// Value Five:
// Specify the field name where where is to be displayed
// the number of characters the user has typed. Make
// it blank (nothing between the quotation marks) if
// you aren't displaying the number of characters typed.
var CharactersTypedFieldName = "CharsTyped";
// Value Six:
// Specify the field name where where is to be displayed
// the number of characters left that may be typed.
// Make it blank (nothing between the quotation marks)
// if you aren't displaying the number of characters
// left.
var CharactersLeftFieldName = "CharsLeft";
// Value Seven:
// Specify the field name where where is to be displayed
// the number of words the user has typed. Make it
// blank (nothing between the quotation marks) if you
// aren't displaying the number of words typed.
var WordsTypedFieldName = "WordsTyped";
// Value Eight:
// Specify the field name where where is to be displayed
// the number of words left that may be typed. Make it
// blank (nothing between the quotation marks) if you
// aren't displaying the number of words left.
var WordsLeftFieldName = "WordsLeft";
//////////////////////////////////////////////////////
// //
// No modfications are required below this point. //
// //
//////////////////////////////////////////////////////
var WordsMonitor = 0;
var MaxWords = parseInt(MaximumWords);
var MaxChars = parseInt(MaximumCharacters);
var textfield = 'document.' + FormName + '.' + TextFieldName + '.value';
function WordLengthCheck(s,l) {
WordsMonitor = 0;
var f = false;
var ts = new String();
for(var vi = 0; vi < s.length; vi++) {
vs = s.substr(vi,1);
if((vs >= 'A' && vs <= 'Z') || (vs >= 'a' && vs <= 'z') || (vs >= '0' && vs <= '9')) {
if(f == false) {
f = true;
WordsMonitor++;
if((l > 0) && (WordsMonitor > l)) {
s = s.substring(0,ts.length);
vi = s.length;
WordsMonitor--;
}
}
}
else { f = false; }
ts += vs;
}
return s;
} // function WordLengthCheck()
function CharLengthCheck(s,l) {
if(s.length > l) { s = s.substring(0,l); }
return s;
} // function CharLengthCheck()
function InputCharacterLengthCheck() {
if(MaxChars <= 0) { return; }
var currentstring = new String();
eval('currentstring = ' + textfield);
var currentlength = currentstring.length;
eval('currentstring = CharLengthCheck(' + textfield + ',' + MaxChars + ')');
if(CharactersLeftFieldName.length > 0) {
var left = 0;
eval('left = ' + MaxChars + ' - ' + textfield + '.length');
if(left < 0) { left = 0; }
eval('document.' + FormName + '.' + CharactersLeftFieldName + '.value = ' + left);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
if(CharactersTypedFieldName.length > 0) {
eval('document.' + FormName + '.' + CharactersTypedFieldName + '.value = ' + textfield + '.length');
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
} // function InputCharacterLengthCheck()
function InputWordLengthCheck() {
if(MaxWords <= 0) { return; }
var currentstring = new String();
eval('currentstring = ' + textfield);
var currentlength = currentstring.length;
eval('currentstring = WordLengthCheck(' + textfield + ',' + MaxWords + ')');
if (WordsLeftFieldName.length > 0) {
var left = MaxWords - WordsMonitor;
if(left < 0) { left = 0; }
eval('document.' + FormName + '.' + WordsLeftFieldName + '.value = ' + left);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
if (WordsTypedFieldName.length > 0) {
eval('document.' + FormName + '.' + WordsTypedFieldName + '.value = ' + WordsMonitor);
if(currentstring.length < currentlength) { eval(textfield + ' = currentstring.substring(0)'); }
}
} // function InputWordLengthCheck()
function InputLengthCheck() {
InputCharacterLengthCheck();
InputWordLengthCheck();
} // function InputLengthCheck()
Head
Paste this code into the HEAD section of your HTML document.
Select code
<script type="text/javascript" src="charLeft.js"></script>
Body
Paste this code into the BODY section of your HTML document
Select code
<form name="myForm">
<textarea cols="30" rows="5" name="TextField"
onBlur="InputLengthCheck();"
onKeyUp="InputLengthCheck();">
</textarea>
<br>
<input readonly type="text" name="CharsTyped" size="8"> characters typed
<br>
<input readonly type="text" name="CharsLeft" size="8"> characters left
<br>
<input readonly type="text" name="WordsTyped" size="8"> words typed
<br>
<input readonly type="text" name="WordsLeft" size="8"> words left
</form>
Leave a Response
(8 comments)If the word limit is reached before the character limit the scritp still allows you to finish the character limit if you use spaces
Consigliere,[p]Thanks! I missed that. It should be fine now.
great code! thank you for help!
In the copy and paste code that is used as an external.js file, there is one mistake. At the end of the initial text block at the top, there is no closing */ This would be placed right after:[br /]"in the code may be removed if desired."
The "which ever comes first" for this script is not true for reaching the word limit first. After the word limit is reached you can still add the remaining characters with a space. Otherwise a great script though.[p]Good Job