0

Hello i am working on a code and i am having some issues.

I created a validation for my form, the form has a drop down menu with 3 options if they choose one of the options, 4 input fields are enabled. I created a funciton to validate those lines that are enabled after selection. Only problem is some how when i choose the other two options (validation should be disable) it should not validate for those input fields, since i will not put any data there. I need help! I want to validate them when they are enable if input fields are disable it should not validate.

Code i have is this.

Fields: Otheremail and other address are the fields that are enabled after selecting one of the options from the drop down menu in the form.

function validate() {
    if($('#response :selected').val() == '0') {
        alert('Debe seleccionar al menos un Agente.');
        return false;
    }

    if($("#otheremail").val()=='')
    {
        alert("Por favor complete todos los campos");
        $("#otheremail").focus();
        return false;
    }

    if($("#otheraddress").val()=='')
    {
        alert("Por favor complete todos los campos");
        $("#otheraddress").focus();
        return false;
    }

};

1 Answer 1

1

You can check whether the option which enables the fields are selected else skip the validation

function validate() {
    //if the option which enables the fields are not selected do not proceed with validation
    if ($('#myselect').val() != 'somevalue') {
        return true;
    }

    if ($('#response :selected').val() == '0') {
        alert('Debe seleccionar al menos un Agente.');
        return false;
    }

    if ($("#otheremail").val() == '') {
        alert("Por favor complete todos los campos");
        $("#otheremail").focus();
        return false;
    }

    if ($("#otheraddress").val() == '') {
        alert("Por favor complete todos los campos");
        $("#otheraddress").focus();
        return false;
    }

};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.