JavaScript Source Code 3000: Forms: Word Count
Word Count
Finally, an easy way to count the number of words that are entered into a form!
JavaScript Source Code 3000: Forms: Word Count
Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Command-s). The script is yours!!!
<!-- TWO STEPS TO INSTALL WORD COUNT:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Shawn Seley -->
<! >
<! >
<!-- Begin
function CountWords (this_field, alertWords, alertChars) {
if (alertWords == null) {
alertWords = true;
}
if (alertChars == null) {
alertChars = false;
}
var fullStr = this_field.value;
var charCount = fullStr.length;
var rExp = /[^A-Za-z0-9]/gi;
var spacesStr = fullStr.replace(rExp, " ");
var cleanedStr = spacesStr + " ";
do {
var old_str = cleanedStr;
cleanedStr = cleanedStr.replace(" ", " ");
} while(old_str != cleanedStr);
var splitString = cleanedStr.split(" ");
var wordCount = splitString.length -1;
if (fullStr.length <1) {
wordCount = 0;
}
if (wordCount == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (charCount == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}
if (alertWords & alertChars) {
alert ("Word Count:\n" + " " + wordCount + wordOrWords + "\n" + " " + charCount + charOrChars);
}
else {
if (alertWords) {
alert ("Word Count: " + wordCount + wordOrWords);
}
else {
if (alertChars) {
alert ("Character Count: " + charCount + charOrChars);
}
}
}
return wordCount;
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<form>
<textarea cols=40 rows=5 name=x>
</textarea>
<br>
<input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);">
</form>
<!-- Script Size: 1.94 KB -->
Wyszukiwarka
Podobne podstrony:
function str word countWord 2013 Keyboard ShortcutsChristmas word challengesfunction mhash countword chop witch brewwordzadania wordTEMAT 2 MiTBNB wordThe L Word [1x12] Locked Up (XviD asd)count ifWordKURS WORD 2007Neu Microsoft Word DokumentGra planszowa Super word racerMS Word dla opornychwordwięcej podobnych podstron