TextArea設(shè)置MaxLength屬性最大輸入值的js代碼
標(biāo)準(zhǔn)的DHTML文檔中TEXTAREA的MAXLENGTH屬性默認(rèn)情況下不起作用,只有當(dāng)事件發(fā)生時才起作用
如下:http://spiderscript.net/site/spiderscript/examples/ex_textarea_maxlength.asp
但TEXT中有且起作用<input type="text" maxlength="20">,
那么在TEXTAREA中怎么實(shí)現(xiàn)輸入內(nèi)容不能超過多少個字符呢。
方法1、如果只需要截取多少個字符的內(nèi)容,則可以:
<textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>
或
<textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea>
方法2、
<script type="text/javascript">
function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}
</script>
<textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea>
這個方法采用截斷法,輸入到最后一個字符的時候如果再輸入則會顯示光標(biāo)閃爍。但可以解決使用CTRL+C復(fù)制過來的長度限制問題,但如果用鼠標(biāo)復(fù)制過來的不還是不行。
方法3、這個方法直接判斷輸入的長度
<script language="javascript" type="text/javascript">
<!--
function imposeMaxLength(Object, MaxLen)
{
return (Object.value.length <MaxLen);
}
-->
</script>
<textarea name="myName" onkeypress="return imposeMaxLength(this, 15);" ></textarea>
當(dāng)輸入內(nèi)容大于15時因?yàn)榉祷貫閒alse所以這個實(shí)現(xiàn)不會顯示光標(biāo)閃爍的問題,但沒有解決復(fù)制過來的長度限制問題即復(fù)制過來的內(nèi)容可以超過最大長度限制
return (Object.value.length <=MaxLen);但我測試發(fā)現(xiàn)當(dāng)輸入字節(jié)數(shù)=maxlen時還可以輸入一個字符,所以我改成 return (Object.value.length <MaxLen);
方法4、其實(shí)方法4是方法2與方法3的基礎(chǔ)上進(jìn)一步優(yōu)化??陀^的說方法2與方法3都只做了一部分工作
<mce:script language="javascript" type="text/javascript"><!--
function textlen(x,y){
var thelength = x.value.length;
window.status=thelength+' of '+y+' maximum characters.';
}
function maxtext(x,y){
tempstr = x.value
if(tempstr.length>y){
x.value = tempstr.substring(0,y);
}
textlen(x,y);
}
// --></mce:script>
<form name="myform">
<textarea name="mytextarea"
cols="45"
rows="3"
wrap="virtual"
onkeypress="return(this.value.length<20)"
onkeydown="textlen(this,20)"
onkeyup="textlen(this,20)"
onblur="maxtext(this,20)"
>
上面的方法在原來的基礎(chǔ)上加了onblur事件,這主要用于處理當(dāng)用戶不是采用輸入而是通過復(fù)制粘貼方法來完成文本的轉(zhuǎn)入時的問題。實(shí)際就是方法2與方法3的結(jié)合版。 以下是我為TextArea增加并利用maxlength屬性及結(jié)合上例的結(jié)果:<html><head><script type="text/javascript">function ismaxlength(obj){var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""if (obj.getAttribute && obj.value.length>mlength)alert('該文本框允許輸入最大長度為'+mlength+"個字符,超出內(nèi)容將會被截斷")obj.value=obj.value.substring(0,mlength)}function imposeMaxLength(obj){ var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "" return (obj.value.length <mlength);}</script></head><body><form name="myform"> <textarea maxlength="5" onkeypress="return imposeMaxLength(this)" onblur="ismaxlength(this)"></textarea></form></body></html>
Javascript代碼
---------------------------------------------------------------------------------------------
function SetTextAreaMaxLength(controlId,length)
{
// JScript File for TextArea
// Keep user from entering more than maxLength characters
function doKeypress(control,length){
maxLength = length;
value = control.value;
if(maxLength && value.length > maxLength-1){
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
event.returnValue = false;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
}
}
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else {
elm['on' + evType] = fn;
}
}
function AttacheventTextAreaBeforePaste(obj,length)
{
return function()
{
doBeforePaste(obj,length)
}
}
function AttacheventTextAreaPaste(obj,length)
{
return function()
{
doPaste(obj,length)
}
}
function AttacheventTextAreaKeyPress(obj,length)
{
return function()
{
doKeypress(obj,length)
}
}
function AttacheventTextAreaDragEnter(obj,length)
{
return function()
{
doDragenter(obj,length);
}
}
var obj = document.getElementById(controlId);
addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);
addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);
addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);
addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null);
}
function SetTextAreaMaxLength(controlId,length)
{
// JScript File for TextArea
// Keep user from entering more than maxLength characters
function doKeypress(control,length){
maxLength = length;
value = control.value;
if(maxLength && value.length > maxLength-1){
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
event.returnValue = false;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
}
}
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else {
elm['on' + evType] = fn;
}
}
function AttacheventTextAreaBeforePaste(obj,length)
{
return function()
{
doBeforePaste(obj,length)
}
}
function AttacheventTextAreaPaste(obj,length)
{
return function()
{
doPaste(obj,length)
}
}
function AttacheventTextAreaKeyPress(obj,length)
{
return function()
{
doKeypress(obj,length)
}
}
function AttacheventTextAreaDragEnter(obj,length)
{
return function()
{
doDragenter(obj,length);
}
}
var obj = document.getElementById(controlId);
addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);
addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);
addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);
addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null);
}
-----------------------------------------------------------------------------------------------
HTML代碼
<asp:TextBox ID="TextBoxAddress" runat="server" Width="200px"
TextMode="MultiLine" Height="113px" MaxLength="10"></asp:TextBox>
<script language="javascript" type="text/javascript">
SetTextAreaMaxLength('<%=TextBoxAddress.ClientID %>',10);
</script>
- 把textarea中字符串里含有的回車換行替換成<br>的javascript代碼
- JS控制文本框textarea輸入字?jǐn)?shù)限制的方法
- javascript textarea字?jǐn)?shù)限制
- javascript textarea光標(biāo)定位方法(兼容IE和FF)
- JavaScript判斷textarea值是否為空并給出相應(yīng)提示
- JavaScript中統(tǒng)計Textarea字?jǐn)?shù)并提示還能輸入的字符
- 在textarea中顯示html頁面的javascript代碼
- JavaScript 獲取/設(shè)置光標(biāo)位置,兼容Input&&TextArea
- 用JavaScript限制textarea輸入長度 (For: IE、Firefox ...)[
- 關(guān)于js對textarea換行符的處理方法淺析
相關(guān)文章
Bootstrap 網(wǎng)站實(shí)例之單頁營銷網(wǎng)站
這篇文章主要介紹了Bootstrap 網(wǎng)站實(shí)例之單頁營銷網(wǎng)站的相關(guān)資料,本文給大家介紹的非常詳細(xì)具有一定的參考借鑒價值,需要的朋友可以參考下2016-10-10理解Javascript_14_函數(shù)形式參數(shù)與arguments
在'執(zhí)行模型詳解'的'函數(shù)執(zhí)行環(huán)境'一節(jié)中對arguments有了些許的了解,那么讓我們深入的分析一下函數(shù)的形式參數(shù)與arguments的關(guān)系。2010-10-10詳解JavaScript實(shí)現(xiàn)動態(tài)的輪播圖效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動態(tài)的輪播圖效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Windows Live的@live.com域名注冊漏洞 利用代碼
Windows Live的@live.com域名注冊漏洞 利用代碼...2006-12-12javascript垃圾收集機(jī)制與內(nèi)存泄漏詳細(xì)解析
本文是對javascript中的垃圾收集機(jī)制與內(nèi)存泄漏進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11基于BootStrap multiselect.js實(shí)現(xiàn)的下拉框聯(lián)動效果
當(dāng)option特別多時,一般的下拉框選擇起來就有點(diǎn)力不從心了,所以使用multiselect是個很好的選擇。在網(wǎng)上找了半天找到了解決方案,具體實(shí)現(xiàn)代碼大家參考下本文吧2017-07-07Javascript實(shí)現(xiàn)的CSS代碼高亮顯示
到網(wǎng)上一位別出心裁的高手使用字符串而不是正則表達(dá)式實(shí)現(xiàn)了Javascript代碼高亮顯示,自己受益匪淺,于是就構(gòu)思了CSS代碼的高亮顯示。2009-11-11全面解析JavaScript中offsetLeft、offsetTop的用法
本文主要介紹了全面解析JavaScript中offsetLeft、offsetTop的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04