Home  >  Article  >  Web Front-end  >  js counts the number of characters entered in the text box_javascript skills

js counts the number of characters entered in the text box_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:35:231215browse

Use JavaScript to calculate the number of characters currently entered by the user in real time. Function code:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>JavaScript统计字符数</title> 
<script language="javascript"> 
function CountWords(obj, show_id){ 
 var fullStr = obj.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; 
  cleanedStrcleanedStr = cleanedStr.replace(' ', ' '); 
 }while( old_str != cleanedStr ); 
  var splitString = cleanedStr.split(' '); 
  document.getElementById(show_id).innerHTML=charCount; 
} 
</script> 
</head> 
<body> 
<form> 
<textarea cols="40" rows="5" name="x" onkeyup="CountWords(this,'show')" /> 
</textarea> 
</form> 
<div id="show">0</div> 
</body> 
</html> 

It is very simple to implement character count statistics in JavaScript. The code shared with you above can be used directly. I hope it will be helpful to your learning.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn