validate校验

//覆写Ext.form.TextField里面的属性
Ext.apply(Ext.form.TextField.prototype,{
    
    //输入是否校验中文
    validateCnLength : true, //false,
    //是否去除两端空格
    //trimValue : true ,
    
    //值校验
    validateValue : function(value){
        if(value.length < 1 || value === this.emptyText){ // if it's blank
             if(this.allowBlank){
                 this.clearInvalid();
                 return true;
             }else{
                 this.markInvalid(this.blankText);
                 return false;
             }
        }
        if(value.length < this.minLength){
            this.markInvalid(String.format(this.minLengthText, this.minLength));
            return false;
        }
        
        //如果validateCnLength为真,验证中英混合字符串,否则验证纯英字符串
        if( this.validateCnLength ? this.cnLength(value) > this.maxLength : value.length > this.maxLength ){
            this.markInvalid(String.format(this.maxLengthText, this.maxLength));
            return false;
        }

        if(this.vtype){
            var vt = Ext.form.VTypes;
            if(!vt[this.vtype](value, this)){
                this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);
                return false;
            }
        }
        if(typeof this.validator == "function"){
            var msg = this.validator(value);
            if(msg !== true){
                this.markInvalid(msg);
                return false;
            }
        }
        if(this.regex && !this.regex.test(value)){
            this.markInvalid(this.regexText);
            return false;
        }
        return true;
    },
    
    //添加获取中英混合字符串长度
    cnLength : function (str) {
        var totallength=0;
        for (var i=0;i<str.length;i++)
        {
            var intCode=str.charCodeAt(i);
            if (intCode >= 0 && intCode <= 128 ) {
                totallength=totallength+1;//非中文单个字符长度加1
            }else {
                totallength=totallength+2;//中文字符长度则加2
            }
        }
        return totallength;
    },
    
    //不能输入空格
    validator : function(text) {
           if (this.allowBlank == false && Ext.util.Format.trim(text).length == 0){
                return false;
           }else{
                return true;
           }
      }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值