Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

Javascript Validation, only run if first choice is true

I am using the below to validate two drop downs. If the first drop down (choice1) is Yes, I want the code to compare that dropdown with the next (choice2). If Choice1 is No, I don't want anything to happen. I know my sample isn't complete... and for some reason after running it changes both choices to be the same. Forgive me as I am not that great with javascript and still learning. Can someone assist?

function DoCustomValidation()
{
  var frm = document.forms["MyForm"];
  if(frm.Choice1.value = frm.Choice2.value)
  {
    sfm_show_error_msg('They match!',frm.Choice1);

    return false;
  }
  else
  {
    return true;
  }
}

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

This is a common mistake:

if(frm.Choice1.value = frm.Choice2.value)

Open in new window


A single = is used for assignment, you need two == to compare:

if(frm.Choice1.value == frm.Choice2.value)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial