Federico
Federico

Reputation: 1

Validate enabled fields in form JAVASCRIPT

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;
    }

};

Upvotes: 0

Views: 111

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

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;
    }

};

Upvotes: 1

Related Questions