Начало
Формы
Количество слов
Легкий способ посчитать число слов, которые введены в форму
JavaScript Forms: Количество слов
<!-- 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"> <! > <! > <!-- 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 -->