Extjs自定义form表单验证

Ext4.0 已经提供了用于扩展表单验证的单例类:

Ext.form.field.VTypes


This is a singleton object which contains a set of commonly used field validation functions. The validations provided are basic and intended to be easily customizable and extended.

To add custom VTypes specify the vtype validation test function, and optionally specify any corresponding error text to display and any keystroke filtering mask to apply



现在我们要做一个text的验证:要求是:输入内容不能超过100个字符,一个汉字抵两个字符,也就是不能超过50个汉字100个字母。

配置项:
{
		            xtype: 'textfield',
		            name: 'foldername',
		            fieldLabel: '目录名称',
		            allowBlank:false,
		            vtype: 'words',
		            wordsLength:100,
		            regex:/^[^\\\/:\*\?|<>"]+$/,
		            regexText:'非法字符\\ | /:*?\"<>',
		            value: ''
		        }


checkWords: function(){
		Ext.apply(Ext.form.field.VTypes, {
		    words: function(val, field) {
		    	var len = field.wordsLength;//获取field中定义的字符长度
		    	val = val.replace(/[\u4e00-\u9fa5]/g,'**');//替换中文为两个*
		    	if(val.length == len){
		    		this.wordsText = "该输入项的最大长度是 "+len+" 个字符"  //设置验证不通过时提示语
		    	}
		        return val.length < len; //true为验证通过,false为不通过
		    }
		});
	}

在引入form之前调用
checkWords()即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值