[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript Validation, only run if first choice is true

Posted on 2012-08-28
2
Medium Priority
?
293 Views
Last Modified: 2012-08-28
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

0
Comment
Question by:earwig75
  • 2
2 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 38341728
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

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 38341738
If the first drop down (choice1) is Yes, I want the code to compare that dropdown with the next (choice2).

if((frm.Choice1.value == 'Yes') && (frm.Choice1.value == frm.Choice2.value))

Open in new window


...which of course only will be true when Choice2 also is 'Yes'.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question