检测输入字符个数
<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 = "个字";
}
else {
wordOrWords = "个字";
}
if (charCount == 1) {
charOrChars = "个字母";
} else {
charOrChars = "个字母";
}
if (alertWords & alertChars) {
alert ("计算结果:n" + "  " + wordCount + wordOrWords + "n" + "  " + charCount + charOrChars);
}
else {
if (alertWords) {
alert ("计算结果: " + wordCount + wordOrWords);
}
else {
if (alertChars) {
alert ("计算结果: " + charCount + charOrChars);
   }
  }
}
return wordCount;
}
// End -->
</script>
<form>
<textarea cols=40 rows=5 name=x>
</textarea>
<br>
<input type=button value="计算字符" OnClick ="CountWords(this.form.x, true, true);">
</form>