Ext JS百強應用:Form驗證、表單驗證、表單錯誤提示信息位置 --第4強

Ext.QuickTips.init(); //爲組件提供提示信息功能,form的主要提示信息就是客戶端驗證的錯誤信息。
Ext.form.Field.prototype.msgTarget='side'; //提示的方式,枚舉值爲
qtip-當鼠標移動到控件上面時顯示提示
title-在瀏覽器的標題顯示,但是測試結果是和qtip一樣的
under-在控件的底下顯示錯誤提示
side-在控件右邊顯示一個錯誤圖標,鼠標指向圖標時顯示錯誤提示. 默認值.
id-[element id]錯誤提示顯示在指定id的HTML元件中
1.一個最簡單的例子:空驗證
//空驗證的兩個參數
1.allowBlank:false//false則不能爲空,默認爲tr
2.blankText:string//當爲空時的錯誤提示信息
js代碼爲:
var form1 = new Ext.form.FormPanel({
width : 350,
renderTo : "form1",
title : "FormPanel",
defaults : {xtype:"textfield",inputType:"password"},
items : [{
id : "blanktest",
fieldLabel : "不能爲空",
allowBlank : false,//不允許爲空
blankText : "不能爲空"//錯誤提示信息,默認爲This field is required!
}]
});
2.用vtype格式進行簡單的驗證。
在此舉郵件驗證的例子,重寫上面代碼的items配置:
items:[{
fieldLabel : "不能爲空",
vtype : "email",//email格式驗證
vtypeText : "不是有效的郵箱地址",//錯誤提示信息,默認值我就不說了
id : "blanktest",
anchor : "90%"
}
你可以修改上面的vtype爲以下的幾種extjs的vtype默認支持的驗證:
//form驗證中vtype的默認支持類型
1.alpha //只能輸入字母,無法輸入其他(如數字,特殊符號等)
2.alphanum //只能輸入字母和數字,無法輸入其他
3.email //email驗證,要求的格式是"[email protected]"

4.url //url格式驗證,要求的格式是[url]http://www.langsin.com[/url] 


3.高級自定義密碼驗證
  前面的驗證都是extjs已經提供的,我們也可以自定義驗證函數。

  //先用Ext.apply方法添加自定義的password驗證函數(也可以取其他的名字) 

        Ext.apply(Ext.form.VTypes,{
            password:function(val,field){      //val指這裏的文本框值,field指這個文本框組件,
                if(field.confirmTo){                    //confirmTo是我們自定義的配置參數,一般用來保存另外的組件的id值
                     var pwd = Ext.getCmp(field.confirmTo); //取得confirmTo的那個id的值 
                     return (val==pwd.getValue()); 
                } 
                return true; 
           } 

        });

//配置items參數
items:[
{
fieldLabel : "密碼",
id : "pass1",
},{
fieldLabel : "確認密碼",
id : "pass2",
vtype : "password",//自定義的驗證類型
vtypeText : "兩次密碼不一致!",
confirmTo : "pass1",//要比較的另外一個的組件的id
}

]


4.使用正則表達式驗證


new Ext.form.TextField({  
    fieldLabel  : "姓名",  
    name        : "author_nam",  
    regex       : /[/u4e00-/u9fa5]/,    //正則表達式在/...../之間. [/u4e00-/u9fa5] : 只能輸入中文.  
    regexText   : "只能輸入中文!",  
    allowBlank  : false                 //此驗證依然有效.不許爲空.  


#####################
Extjs 常用 vtype 列表
Ext.form.VTypes["hostnameVal1"] = /^[a-zA-Z][-.a-zA-Z0-9]{0,254}$/;
Ext.form.VTypes["hostnameVal2"] = /^[a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}([.][a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}){0,}$/;
Ext.form.VTypes["ipVal"] = /^([1-9][0-9]{0,1}|1[013-9][0-9]|12[0-689]|2[01][0-9]|22[0-3])([.]([1-9]{0,1}[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2}[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-4])$/;
Ext.form.VTypes["netmaskVal"] = /^(128|192|224|24[08]|25[245].0.0.0)|(255.(0|128|192|224|24[08]|25[245]).0.0)|(255.255.(0|128|192|224|24[08]|25[245]).0)|(255.255.255.(0|128|192|224|24[08]|252))$/;
Ext.form.VTypes["portVal"] = /^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
Ext.form.VTypes["multicastVal"] = /^((22[5-9]|23[0-9])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3})|(224[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2})|(224[.]0[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])))$/;
Ext.form.VTypes["usernameVal"] = /^[a-zA-Z][-_.a-zA-Z0-9]{0,30}$/;
Ext.form.VTypes["passwordVal1"] = /^.{6,31}$/;
Ext.form.VTypes["passwordVal2"] = /[^a-zA-Z].*[^a-zA-Z]/;
Ext.form.VTypes["hostname"] = function(v){
if(!Ext.form.VTypes["hostnameVal1"].test(v)){
Ext.form.VTypes["hostnameText"]="Must begin with a letter and not exceed 255 characters"
return false;
}
Ext.form.VTypes["hostnameText"]="L[.L][.L][.L][...] where L begins with a letter, ends with a letter or number, and does not exceed 63 characters";
return Ext.form.VTypes["hostnameVal2"].test(v);
}
Ext.form.VTypes["hostnameText"] = "Invalid Hostname"
Ext.form.VTypes["hostnameMask"] = /[-.a-zA-Z0-9]/;
Ext.form.VTypes["ip"] = function(v){
return Ext.form.VTypes["ipVal"].test(v);
}
Ext.form.VTypes["ipText"] = "1.0.0.1 - 223.255.255.254 excl ing 127.x.x.x"
Ext.form.VTypes["ipMask"] = /[.0-9]/;
Ext.form.VTypes["netmask"] = function(v){
return Ext.form.VTypes["netmaskVal"].test(v);
}
Ext.form.VTypes["netmaskText"] = "128.0.0.0 - 255.255.255.252"
Ext.form.VTypes["netmaskMask"] = /[.0-9]/;
Ext.form.VTypes["port"] = function(v){
return Ext.form.VTypes["portVal"].test(v);
}
Ext.form.VTypes["portText"] = "0 - 65535"
Ext.form.VTypes["portMask"] = /[0-9]/;
Ext.form.VTypes["multicast"] = function(v){
return Ext.form.VTypes["multicastVal"].test(v);
}
Ext.form.VTypes["multicastText"] = "224.0.1.0 - 239.255.255.255"
Ext.form.VTypes["multicastMask"] = /[.0-9]/;
Ext.form.VTypes["username"] = function(v){
return Ext.form.VTypes["usernameVal"].test(v);
}
Ext.form.VTypes["usernameText"] = "Username must begin with a letter and cannot exceed 255 characters"
Ext.form.VTypes["usernameMask"] = /[-_.a-zA-Z0-9]/;
Ext.form.VTypes["password"]=function(v){
if(!Ext.form.VTypes["passwordVal1"].test(v)){
Ext.form.VTypes["passwordText"]="Password length must be 6 to 31 characters long";
return false;
}
Ext.form.VTypes["passwordText"]="Password must incl e atleast 2 numbers or symbols";
return Ext.form.VTypes["passwordVal2"].test(v);
}
Ext.form.VTypes["passwordText"] = "Invalid Password"
Ext.form.VTypes["passwordMask"] = /./;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章