textarea文字输入字数限制,不超300
<div class="text"> <textarea placeholder="请填写其他建议..." onkeyup="checkLen(this)" id="test"></textarea> <div class="tet"><span id="count">300</span><i><img src="img/voice.png" alt=""/></i></div> <div class="clear"></div> </div>
function checkLen(obj) { var maxChars = 300;//最多字符数 if (obj.value.length > maxChars) obj.value = obj.value.substring(0,maxChars); var curr = maxChars - obj.value.length; document.getElementById("count").innerHTML = curr.toString(); }
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery限制输入字数,并提示剩余字数</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function words_deal() { var curLength=$("#TextArea1").val().length; if(curLength>5) { var num=$("#TextArea1").val().substr(0,5); $("#TextArea1").val(num); alert("超过字数限制,多出的字将被截断!" ); } else { $("#textCount").text(5-$("#TextArea1").val().length); } } </script> </head> <body> 剩余<span id="textCount">5</span>个字<br /> <textarea name="textarea" id="TextArea1" cols="45" rows="5" onkeyup="words_deal();" ></textarea> </body> </html>
3
<input name="text" id="text" type="text" value="" size="38" class="input"> <script language="JavaScript" type="text/javascript"> str=getElementById('text').value if(str.length<6){ return false; alert('小于六位'); } </script>
还没有评论,来说两句吧...