javascript限定输入textarea输入长度

<textarea  id="content" onkeyup="limitLength(this.value,'512','content')"></textarea>

//限定输入textarea输入长度,一个汉字作为三个字节
function limitLength(value, byteLength, selectorId){  
    var newvalue = value.replace(/[^\x00-\xff]/g, "**");    
    var length = newvalue.length;    
    //当填写的字节数小于设置的字节数    
    if(length * 1 <=byteLength * 1){    
        return false;    
    }    
    var limitDate = newvalue.substr(0, byteLength);    
    var count = 0;    
    var limitvalue = "";    
    for(var i = 0; i < limitDate.length; i++){    
        var flat = limitDate.substr(i, 1);    
        if(flat == "*"){   
            count++; 
        }  
    }   
    var size = 0;    
    var istar = newvalue.substr(byteLength * 1 - 1, 1);//校验点是否为“×”    
    //if 基点是×; 判断在基点内有×为偶数还是奇数     
    if(count % 2 == 0){    
        //当为偶数时    
        size = count / 2 + (byteLength * 1 - count);    
        limitvalue = value.substr(0, size);    
    }else{   
        //当为奇数时    
        size = (count - 1) /2 + (byteLength * 1 - count);    
        limitvalue = value.substr(0, size);    
    }
    document.getElementById(selectorId).value = limitvalue;
    return false;
}

 

posted @ 2017-03-28 17:53  N神3  阅读(671)  评论(0编辑  收藏  举报