`
happyqing
  • 浏览: 3152082 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

为form中的文本框绑定验证最大长度事件(按字节)

    博客分类:
  • js
阅读更多

 

/**  
 * 为form中的文本框绑定验证最大长度事件(按字节)  
 *   
 * @param fromName  
 */  
function bindMaxLength(fromName,height){   
    if(typeof(height) == "undefined"){   
        height = 16;   
    }   
    $("form[name="+fromName+"] input[type=text]").each(function(){   
        if(typeof ($(this).attr("maxlength"))!="undefined"){   
//          alert("width"+this.style.width);   
//          alert("width"+$(this).css("width")); //会取到默认宽度   
            //解决ie8下onpropertychange事件间歇性失效的问题   
            if(this.style.width != "" && this.style.height==""){   
                this.style.height = height+"px";   
            }   
            $(this).bind('input propertychange', function() {   
                    var inputLength = this.value.replace(/[^\x00-\xff]/g, 'xxx').length;   
                    var maxlength = parseInt($(this).attr("maxlength"));   
                    if(inputLength > maxlength){   
                        this.value = this.value.subStringByBytes(maxlength);   
                    }   
            });    
        }   
    });   
}  

subStringByBytes

JS按字节计算字符串长度,按字节截取字符串

http://happyqing.iteye.com/blog/1979816

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics