amit sutar
amit sutar

Reputation: 541

How to apply if else condtion in jquery script with selected value from dropdown?

I am working on one form. Applying some java script validations on form. I want one changes with my validations. I have one dropdown this some company names. suppose i have five companies .

<select id="org_id">
    <option value="1">ABC</option>
    <option value="2">DEF</option>
    <option value="3">GHI</option>
    <option value="4">IJK</option>
    <option value="5">LMN</option>
</select> 

if i want to not validate form if user select ABC and if user will select any other then only validation should be apply on form.

My validation script:

var frmvalidator = new Validator("add_employee_frm");
    frmvalidator.addValidation("employee_number","req","Please Enter Employee Number");
    frmvalidator.addValidation("employee_name","req","Please Enter Employee Name");
    frmvalidator.addValidation("employee_name","alphabetic_space","Please Enter Valid Employee Name"); 

I did try with if else condition on my validation script. but its not working.

$(function () {
    $("#org_id").change(function () {                   
        var selectedValue = $(this).val();
        alert(selectedValue);
    });
});
    if(selectedValue == 1)
    {
        return true;
    }
    elseif(selectedValue != 1)
    {
        var frmvalidator = new Validator("add_employee_frm");
        frmvalidator.addValidation("employee_number","req","Please Enter Employee Number");
        frmvalidator.addValidation("employee_name","req","Please Enter Employee Name");
        frmvalidator.addValidation("employee_name","alphabetic_space","Please Enter Valid Employee Name");
        return true;
    }

this code is not working for me. i am getting value from selected value and pushing it into if..else condition. how to achieve this .

Upvotes: 0

Views: 91

Answers (3)

Nitish Jha
Nitish Jha

Reputation: 1

Hi you need to add the if-else check inside onchange function to get the value everytime dropdown is changed. In your case you are declaring if else after change and ready because of which selectedValue will never get the value.

$(function () {
    $("#org_id").change(function () {                   
        var selectedValue = $(this).val();
        alert(selectedValue);

        if(selectedValue == 1)
        {
            return true;
        }
        elseif(selectedValue != 1)
        {
            var frmvalidator = new Validator("add_employee_frm");
            frmvalidator.addValidation("employee_number","req","Please Enter Employee Number");
            frmvalidator.addValidation("employee_name","req","Please Enter Employee Name");
            frmvalidator.addValidation("employee_name","alphabetic_space","Please Enter Valid Employee Name");
            return true;
        }
    });
});

Upvotes: 0

fayis003
fayis003

Reputation: 680

as far as I understand what you need is to bind the validation code if the user selected a specific value from the drop-down. and here the main issue is the first block of code you have executed inside the document ready and rest outside of it try like below.

$(function(){
  if($('#org_id').val() == 1){
      var frmvalidator = new Validator("add_employee_frm");
      frmvalidator.addValidation("employee_number", "req", "Please Enter Employee Number");
      frmvalidator.addValidation("employee_name", "req", "Please Enter Employee Name");
      frmvalidator.addValidation("employee_name", "alphabetic_space", "Please Enter Valid Employee Name");
  }
});

Upvotes: 1

void
void

Reputation: 36703

You do not need onchange function. Just calculate the value of the dropdown and compare it in the if condition only.

if ($("#org_id").val() == 1) {
  return true;
}
else{
  var frmvalidator = new Validator("add_employee_frm");
  frmvalidator.addValidation("employee_number", "req", "Please Enter Employee Number");
  frmvalidator.addValidation("employee_name", "req", "Please Enter Employee Name");
  frmvalidator.addValidation("employee_name", "alphabetic_space", "Please Enter Valid Employee Name");
  return true;
}

And to be specific in your code the problem was scoping of your variables. As in onchange the variable selectedValue is scoped to its enclosing function and is inaccessible by any other block.

Upvotes: 0

Related Questions