Validation Triggering Twice

Hi Plumsail.

Hope you are well.

I have a choice field with various options. When the user selects an option, the form dynamically hides some grids which contain fields.

I want to turn off the validation for the fields in grids that are hidden when I select an option from choice field.

I've added these two snippets of code in my JS editor.

When I select the "I want to promote" option in the choice field, both errors messages appear on the form, when only one error message should appear.

//Supervisor Email Address Validation - [Wizard 2]
fd.field('Supervisor_Email_Address').validators.push({
    name: '',
    error: "Please enter supervisor email",
    validate: function(value) {
        if (
        fd.field('Question_1').value == "I want to Update"
        && (/^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)/i.test(value)))
    
        {
            return true;
        }
        return false;
    }
});

//Supervisor Email Address Validation - [Wizard 2]
fd.field('Supervisor_Email_Address').validators.push({
    name: '',
    error: "Please enter supervisor email",
    validate: function(value) {
        if (
        fd.field('Question_1').value == "I want to Promote"
        && (/^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)/i.test(value)))
    
        {
            return false;
        }
        return true;
    }
});

How do you get the form to trigger only one error message when the exact option is selected above?
Is there a better way to write the code above to catch all the options?

Thanks a lot for your help as always!

Hi @DryChips,

Each validation function added to the field is triggered separately. Try using one function for both cases:

if ((fd.field('Question_1').value == "I want to Update" || fd.field('Question_1').value == "I want to Promote") && (/^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)/i.test(value))) {
    return false;
}

This statement will be triggered if Question_1 is "I want to Update" or "I want to promote" using the || operator.

Hiya, thanks for replying.

Unfortunately, it still triggers when the statement is not "I want to promote" or "I want to update".

For example, if I select the option "I want to Freeze" or "I want to Terminate" the Supervisor Email address field still triggers an error. How do I switch this off?

//Supervisor Email Address Validation - [Wizard 2]
fd.field('Supervisor_Email_Address').validators.push({
    name: '',
    error: "Please enter",
    validate: function(value) {
        if (
            
            (  fd.field('Question_1').value == "I want to Update" 
            || fd.field('Question_1').value == "I want to Promote"
            ) 
            && (/^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)/i.test(value))) 
    {
            return true;
        }
    }
});

Hi @DryChips,

You need to return true in cases where validation shouldn't be triggered. Try this:

fd.field('Supervisor_Email_Address').validators.push({
    name: '',
    error: "Please enter",
    validate: function(value) {
        if (fd.field('Question_1').value == "I want to Update" || fd.field('Question_1').value == "I want to Promote") {
            return /^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)/i.test(value);
        }
        return true;
    }
});

By the way, by adding $ at the end of the regex like this

/^[A-Za-z0-9._%'+-]+@(?:google.com|yahoo.uk|outlook.uk)$/

you can ensure that emails with trailing symbols like example@google.comsomething are not recognized as correct.