user
user

Reputation: 619

additonal parameters for custom_func?

I want to reuse the following code for custom_func:

function validLen(value,colName){
    if(value.length === 8){
        return [true,""];
    }
    else{
        return [false,"fail"];
    }
}

I tried giving it an additional parameter as follows:

function validLen(value,colName,length){
    if(value.length === length){
        return [true,""];
    }
    else{
        return [false,"fail"];
    }
}

And calling it like this:

{name:'cntrct_id', editrules:{custom: true, custom_func:validLen(8)} },

Didn't work. The previous code DOES work, but as stated, I want a reusable function. Is there a workaround for this? Am I doing it wrong?

Upvotes: 3

Views: 5194

Answers (1)

Oleg
Oleg

Reputation: 221997

I would recommend you to use

editoptions: { maxlength: 8}

instead of the custom validation which you use. In the case the input element will be created with the maxlength attribute directly. So the user will not able to type more as characters as specified by maxlength.

UPDATED: You can't change the interface of any callback function, but you can do share common code of different custom_func in the following way. You define your custom validation function having three parameters like

function validLen (value, colName, valueLength) {
    if (value.length === valueLength) {
        return [true, ""];
    }
    else {
        return [false, "fail"];
    }
}

and use it in the following way

{
    name: 'cntrct_id',
    editrules: {
        custom: true,
        custom_func: function (value, colName) {
            return validLen(value, colName, 8);
        }
}

If you need to use this inside of custom_func then you can change return validLen(value, colName, 8); to return validLen.call(this, value, colName, 8);.

Upvotes: 5

Related Questions