19eggs
19eggs

Reputation: 195

Radio Button and Dropdown Validation

What I am trying to do is, on clicking the save button, if a user has chosen any Missing Radio Button but have not chosen a drop down option, then an alert message needs to show.

I tried 2 approaches. First is this way

if ($(".missing input[type:radio]:checked").length > 0 && $(".reason").val() == "")
  { 
   alert("You have not selected a reason from the drop down list of the Missing section.");
  return false;
  }

Second method -- by identifying the text in the textarea. If it contains ~CM~~ then show an alert

if ("ta:contains(~CM~~).length>0")
{
alert("Missing radio has been selected but nothing in the drop down has been selected");
}

I have commented out the code in the fiddle and feel to use any method.

JSFIDDLE

Upvotes: 0

Views: 50

Answers (1)

Bla...
Bla...

Reputation: 7288

First of all, it's not a good approach to use <table> to design your site, because the crawler won't able to get any info from your web.. Even for this issue, I added dummy class to your <tr> that has those radio buttons..

Change your 2 approaches into this:

//for checking the combobox
$('.selection').each(function(){
    if($(this).find(".missing input").attr("checked") == "checked" && $(this).find(".reason").val() == ""){
        alert("You have not selected a reason from the drop down list of the Missing section.");
    }
});
//for checking the textarea
$('.selection').each(function(){
    if($(this).find(".missing input").attr("checked") == "checked" && $(this).find("textarea").val() == ""){
        alert("You have not type anything in the textarea.");
    }
});

Check out this Fiddle.

Upvotes: 1

Related Questions