TextArea设置MaxLength属性最⼤输⼊值的js代码
复制代码代码如下:
textarea中cols表示<textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>
或
复制代码代码如下:
<textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea>
、
复制代码代码如下:
<script type="text/javascript">
function ismaxlength(obj){
var Attribute? Attribute("maxlength")) : ""
if (Attribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}
</script>
<textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea>
这个⽅法采⽤截断法,输⼊到最后⼀个字符的时候如果再输⼊则会显⽰光标闪烁。但可以解决使⽤CTRL+C复制过来的长度限制问题,但如果⽤⿏标复制过来的不还是不⾏。
、这个⽅法直接判断输⼊的长度
复制代码代码如下:
<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>
当输⼊内容⼤于15时因为返回为false所以这个实现不会显⽰光标闪烁的问题,但没有解决复制过来的长度限制问题即复制过来的内容可以超过最⼤长度限制
return (Object.value.length <=MaxLen);但我测试发现当输⼊字节数=maxlen时还可以输⼊⼀个字符,所以我改成 return (Object.value.length <MaxLen);
、其实⽅法4是⽅法2与⽅法3的基础上进⼀步优化。客观的说⽅法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)"
>
上⾯的⽅法在原来的基础上加了onblur事件,这主要⽤于处理当⽤户不是采⽤输⼊⽽是通过复制粘贴⽅法来完成⽂本的转⼊时的问题。实际就是⽅法2与⽅法3的结合版。以下是我为TextArea增加并利⽤maxlength属性及结合上例的结果:<html>
<head><script type="text/javascript">function ismaxlength(obj){var Attribute?
Attribute("maxlength")) : ""if (Attribute && obj.value.length>mlength)alert('该⽂本框允许输⼊最⼤长度为'+mlength+"个字符,超出内容将会被截断")obj.value=obj.value.substring(0,mlength)}function imposeMaxLength(obj){ var Attribute? Attribute("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>
---------------------------------------------------------------------------------------------
复制代码代码如下:
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){
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
maxLength = parseInt(maxLength);
var oTR = control.ateRange();
var iInsertLength = maxLength - value.length + length;
var sData = Data("Text").substr(0,iInsertLength);
< = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
}
}
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 = ElementById(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){
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
maxLength = parseInt(maxLength);
var oTR = control.ateRange();
var iInsertLength = maxLength - value.length + length;
var sData = Data("Text").substr(0,iInsertLength); = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
}
}
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 = ElementById(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);
}
-----------------------------------------------------------------------------------------------
复制代码代码如下:
<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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论