views:

37

answers:

2

Hi, I am new to extjs. I like to know, Is it possible to do dynamic vtype validation like below code...

customRegEX = /^[a-z0-9]/i

customMsg = 'Must be an alphanumeric word'


function ConstructVtype(customRegEX,customMsg)
{
    var custExp = customRegEX;
    Ext.apply(Ext.form.VTypes, {
        AlphaNum: function(v,field) {   
            return /^[a-z0-9]/i.test(v); // instead of this code
            return custExp.test(v);
        },
        AlphaNumText: customMsg,
        AlphaNumMask: custExp
    });
}

But I am getting error (Object does not support this method) in the line return custExp.test(v); since there is no method called test in the object(custExp)

Is it possible to typecast custExp to that object which hold the test method,

If the above point is possible means pls provide that object type and how to typecast ? OR Provide how can I achieve this functionality in different way.

hi "Alexander Gyoshev" thanks for ur replay

if i do as per u recommended its working man but i need to change the regexp value dynamically by the textfield change, like the below code how can do this man

function ConstructVtype()
{   
    var customRegEX = this.customRegEX;  ////^[a-z0-9]/i,
    customMsg =this.customErrorMsg;

    Ext.apply(Ext.form.VTypes, {
        AlphaNum: function(v,field) {
            return customRegEX.test(v);
        },
        AlphaNumText: customMsg,
        AlphaNumMask: customRegEX
    }); 

}


var txt = new Ext.form.TextField({
 renderTo:Ext.getBody(),
  validator :ConstructVtype,
  fieldLabel: 'Telephone',
  name: 'Telephone',
  vtype:'AlphaNum',
  id:'test1',
  customRegEX:'/^[a-z0-9]/i',
  customErrorMsg:'Must be an alphanumeric word',
  width:240  

});

var txt2 = new Ext.form.TextField({
 renderTo:Ext.getBody(),
  validator :ConstructVtype,
  fieldLabel: 'Telephone',
  name: 'Telephone',
  vtype:'AlphaNum',
  id:'test2',
  customRegEX:'/^[a-zA-Z]/i',
  customErrorMsg:'Must be an alphabets',
  width:240  

});

Thanks in advance

+1  A: 

The parameters of the function override the global ones. You can refactor the above code in the following way:

var customRegEX = /^[a-z0-9]/i,
    customMsg = 'Must be an alphanumeric word';


function ConstructVtype()
{
    Ext.apply(Ext.form.VTypes, {
        AlphaNum: function(v,field) {
            return customRegEX.test(v);
        },
        AlphaNumText: customMsg,
        AlphaNumMask: customRegEX
    });
}
Alexander Gyoshev
hi alex thnks for ur reply ...i need a way out using this method ..pls check the question updated one ...
vineth
+1  A: 
var customRegEX = new RegExp('^[a-z0-9]',i);

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

[edited]

Of course I forgot to put '' around regexp string.

Mchl
hey Mchl, thnk for ur reply man ..can u view my updated question... how can i use that common method for all textbox in the window or the form.
vineth