sheela balaji
sheela balaji

Reputation: 119

Error in setting alert in radiobuttonlist?

I have a two text box with radiobuttonlist.If the two test are blank it should alert "textbox are blank" and if its not blank it should alert "date is enabled".

This what i have tried with javascript

 function checked() {
        var radio = document.getElementById('<%=rbtn1.ClientId%>');
        var cal1 = document.getElementById('<%=textstqo.ClientId%>').value;
        var cal2 = document.getElementById('<%=textedqo.ClientId%>').value;
    if (radio.checked = true) {

         if(cal1 == '' && cal2 =='')
        {
        alert("dates cannot be blank");
        return false
        }else
        {
        alert("dates are enabled");
         return true
        }
        }

    }

Radio button list

<asp:RadioButtonList ID="rbtn1" runat="server" RepeatDirection="Vertical" RepeatLayout="Table" Height="300px" Width="101px" onClick="checked()" >
 <asp:ListItem Text="one" Value="one" Enabled="true"></asp:ListItem>
 <asp:ListItem Text="two" Value="two" Enabled="true" ></asp:ListItem>
<asp:ListItem Text="three" Value="three" Enabled="true"></asp:ListItem>

Edited Error: Function expected

The first alert is working .when i tried to check the alert in else part it shows the above error I have only shown you the first radio button that is "quaterone" and the javascript is also for quaterone

Can anyone tell me what i am doing wrongly.Thanks in advance

Upvotes: 2

Views: 48

Answers (2)

user2575725
user2575725

Reputation:

Problem is you are using wrong id for the radio button. '<%=rbtn1.ClientId%>_Quaterone'

Try instead: '<%=rbtn1.ClientId%>'

Edited:

var radioCont = document.getElementById('<%=rbtn1.ClientId%>');
var radio = radioCont.getElementsByTagName('input')[0];//first radio

Also you are trying to assign value instead of comparison:

if (radio.checked = true) { //assignment

Try this:

if (radio.checked == true) { // though `===` is recommended over `==`

You may also try:

if(radio.checked) {
  // to do here
}

Change the attribute to onchange instead of onclick, here:

onClick="checked()" >

In order to uncheck the radio assign false to its state:

alert("dates cannot be blank");
radio.checked = false; //un-check the radio
return false;

Upvotes: 1

shanish
shanish

Reputation: 1984

To validate only when the first radio button is checked, you can check it like

if($('#rbtn1 input:radio:checked').val() == 'quaterone'){
    // your condition here...
}

Upvotes: 0

Related Questions