2014-10-10 49 views
0

我目前試圖驗證字符串格式EXTJS 5驗證 - 驗證後的4個字符輸入

1字符(AZ)和3個字符(AZ 0-9)(字母字符,然後進行3個字母數字)

這是我的自定義V型:

customalphanumeric: function(
    return (/^([A-Za-z]){1}([A-Za-z0-9]){3}$/).test(v); 
}, 
customalphanumericText: 'Please enter 1 Alpha character followed by 3 alphanumeric characters', 
customalphanumericMask: /([A-Za-z0-9])/ 

當我輸入任何字母到框中的文本顯示了「請輸入1個字母字符後跟3個字母數字字符」。

如何讓它等待輸入4個字符?

我試過這樣:

if (v.length == 4) { 
    return (/^([A-Za-z]){1}([A-Za-z0-9]){3}$/).test(v); 
} 
else { 
    return true; 
} 

但允許少於4個字符的短語。任何幫助將非常感激。

回答

1

我不認爲這是要去工作,你所希望的方式,因爲如果沒有顯示錯誤,直到它達到4個字符這意味着表單在此之前是有效的,並且使用表單的任何人都可以提交表單。

你可以做的是添加的minLength:4財產,並得到所有的驗證正確顯示:

Ext.application({ 
    name : 'MyApp', 

    launch : function() { 
     var customRegEx = /^([A-Z]){1}([A-Z0-9]){3}$/i; 
     Ext.apply(Ext.form.field.VTypes, { 
      customtest: function(val, field) { 
       return customRegEx.test(val); 
      }, 
      customtestText: 'Please enter 1 Alpha character followed by 3 alphanumeric characters', 
      customtestMask: /([A-Z0-9])/ 
     }); 

     Ext.create('Ext.panel.Panel',{ 
      title: 'Test Panel', 
      renderTo: Ext.getBody(), 
      items: [{ 
       xtype: 'textfield', 
       vtype: 'customtest', 
       minLength: 4 
      }] 
     }) 
    } 
}); 
+0

我明白你在說什麼。這是一個更好的解決方案。謝謝。 – user4067565 2014-10-13 12:04:39

0

你幾乎擁有它。

if (v.length == 4) { 
    return (/^([A-Za-z]){1}([A-Za-z0-9]){3}$/).test(v); 
} 
else { 
    //return true; 
    return false; 
} 

你可以改變還給else語句的文本以及