Javascript implements a word limit for writing characters to text fields

  • 2020-03-30 01:43:21
  • OfStack

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 
<html xmlns=""> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> Headless document </title> 

<textarea id="text"cols="20" rows="5" onkeyup="sum ()" onfocus="focu()"></textarea> 
<span id="span"></span> 
function sum(){ 
var str =document.getElementById("text"); 
var cont=str.value; 
var len=cont.length; 
document.getElementById("span").innerHTML=" You are currently typing "+len+" A character "; 
if(len>=100){alert(" You can't write anymore, it's maximum! "); 
//Achieve a maximum of 100 words, if more than that, do not write data!

Related articles: