Additional options 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 to give it an additional parameter like this:

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)} },

      

Does not work. The previous code works, but as pointed out, I want to use the reusable feature. Is there a workaround for this? Am I doing it wrong?

+3


source to share


1 answer


I would recommend that you use

editoptions: { maxlength: 8}

      

instead of the custom validation you are using. In case the input element will be created using the maxlength attribute . Thus, the user will not be able to type more characters specified in maxlength

.

UPDATED . You cannot change the interface of any callback function, but you can make the general code of another custom_func

like this. You define your custom validation function that has three parameters like



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

      

and use it like this

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

      

If you need to use this

internally custom_func

, you can change return validLen(value, colName, 8);

to return validLen.call(this, value, colName, 8);

.

+5


source







All Articles