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

ExtJs的TextField内容长度限制(中文全角为2个长度)

    博客分类:
  • ext
阅读更多

           extjs版本:3.3

           ext的TextField本身自带长度校验,不过是按照字符的长度进行计算,汉字或者全角符号都只计算一个长度,由于项目需要,需要按照两个长度进行计算,可以通过修改Ext的源码进行修改,在ext源码包的TextField定义的地方,找到getErrors方法,找到长度比较的位置,如下

 

		if (value.length < this.minLength) {
			errors.push(String.format(this.minLengthText, this.minLength));
		}

		if (value.length > this.maxLength) {
			errors.push(String.format(this.maxLengthText, this.maxLength));
		}

 

将其修改为:
	/**
	 * 将字符长度转换为字节长度,汉字以及全角字符占2字节
	*/
	var valueLength = 0;
        for (var i = 0; i < value.length; i++) {
            var c = value.charCodeAt(i);
            //根据单双字节进行长度计算,单字节+1
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                valueLength++;
            }else {
                valueLength += 2;
            }
        }
	if (valueLength < this.minLength) {
		errors.push(String.format(this.minLengthText, this.minLength));
	}

	if (valueLength > this.maxLength) {
		errors.push(String.format(this.maxLengthText, this.maxLength));
	}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics