您的位置:首页 > Web前端 > JavaScript

Extjs自定义form表单验证

2011-08-25 15:31 1026 查看
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()即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: