Home > Software engineering >  jquery validation addmethod with dynamic error message
jquery validation addmethod with dynamic error message

Time:09-28

I am using jquery.validation to validate a date of birth if entered. I am using the Add method but want to customise the error message depending on what is wrong with the value entered. I am using moment.js to do the date validation .

So the first check should check it is valid. Then I want to check if the person is less than 1 or older than 110 and display an appropriate error message if not.

I have tried setting a global variable, but because the message is set in the constructor this is invalid - but I have left it in here to show what I have tried.

My code is as follows

var err = 'error date'; /* using this method does not work */

jQuery.validator.addMethod("validDOB", function(value, element) {
            
    if (value != "") {
        try {
            if (!moment(value,"DD-MM-YYYY",true).isValid()) {
                throw "Please enter a valid date (dd-mm-yyyy)!";
            }
            
            var years = moment().diff(moment(value, "DD-MM-YYYY"), 'years');
            
            if (years < 1) {
                throw "You seem a little young";
            }
            
            if (years > 110) {
                throw "You seem a little old";
            }
                
        }
        catch (e) {
            err = e;
            return false;
        }
    }
    
    return true;
}, err);

function setupValidation()
{   
    registerValidator = $("#UserReg").validate({
        rules: {
            txtRegUserDOB: {
                validDOB: true
            }
        }
    });
}

setupValidation();

Is there a way to generate a custom error message like this?

CodePudding user response:

You can define custom error messages as below:

jQuery.validator.addMethod("validDOB", function(value, element) {                
       if (value != "") {
            if (!moment(value,"DD-MM-YYYY",true).isValid()) {
                return false;
            }                
            var years = moment().diff(moment(value, "DD-MM-YYYY"), 'years');                
            if (years < 1) {
                return false; 
            }                
            if (years > 110) {
                return false;
            }                    
        }           
    return true;
}, function(error, element){
    var value = $(element).val();
    if (!moment(value,"DD-MM-YYYY",true).isValid()) {
        return "Please enter a valid date (dd-mm-yyyy)!";
    }
    
    var years = moment().diff(moment(value, "DD-MM-YYYY"), 'years');
    
    if (years < 1) {
        return "You seem a little young";
    }
    
    if (years > 110) {
        return "You seem a little old";
    }
});

function setupValidation()
{   
    registerValidator = $("#UserReg").validate({
        rules: {
            txtRegUserDOB: {
                validDOB: true
            }
        }
    });
}

setupValidation();
  • Related