Extjs 前端校验的一些方式总结

来源:互联网 发布:英雄联盟竞猜软件 编辑:程序博客网 时间:2024/05/18 20:10

1)、文本框:

输入不能为空:设置配置项allowBlank:false;

输入最大长度:maxLength:Number ;

输入最小长度:minLength:Number ;

不能输入负数:allowNegative:false;

不能输入小数:allowDecimals:false;

规定输入数字范围:minValue和maxValue;

2)、借助vtype:

只能输入英文字母:vtype:’alpha’;

只能输入英文字母和数字:vtype:’alphanum’;

电子邮箱: vtype:’email’;

网址:vtype:’url’;

3)、生日验证:

Ext.apply(Ext.form.VTypes,{

           birthdayCheck:function(val,field){

              if(field.confirmTo){

                  varcheckBirthday = setForm.getForm().findField('identityNum').getValue();

                  var strBirthday= checkBirthday.substring(6,14);

                  var t =val.split('-');

                  var year =t[0];

                  var month =t[1];

                  var day = t[2];

                  var time = year+ month + day;

                  return(strBirthday == time);

              }

              return true;

           }

       });

{

Items:[{xtype:'textfield'

           ,id:'identityId'

           ,fieldLabel:'身份证号'

           ,name:'identityNum'

           ,anchor:'95%'

       },{

           xtype:'datefield'

           ,fieldLabel:'生日'

           ,id:'birthdayId'

           ,name:'birthday'

           ,vtype:'birthdayCheck'

           ,vtypeText:'生日填写错误'

           ,confirmTo:'identityId'

           ,format:'Y-m-d'

           ,anchor:'95%'

           }]

4)、密码验证:

//添加自定义的password验证函数

Ext.apply(Ext.form.VTypes,{

    password:function(val,field){//value指文本框的值,field指文本框组件

       if(field.confirmTo){

           varpwd=Ext.get(field.confirmTo);//confirmTo指自定义的配置参数,一般用来保存另外的组件的id值

           return(val==pwd.getValue());

       }

       return true;

    }

});

       items:[{

              id:"pass1",

              fieldLabel:'密码',

              inputType:'password'

       },{fieldLabel:'确认密码',

           id:"pass2",

           inputType:'password',

           vtype:'password',

           vtypeText:'两次密码不一致',

           confirmTo:"pass1"

       }]