• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Validation of textbox on "checkbox.checked==true" and radio buttons selection

Hello all, I'm new to Asp.Net and doing form validations. I got few problems in validation and would be thankful if someone replies:

1) I am using AutoPostBack=true and then call my if conditions from CheckedChanged funtion ,for checkboxes and radio buttons selection events to get fired. Is there any other way to make selection without invoking AutoPostBack because it refreshes the form and if there is a password, it removes.

2) On checkbox.checked==true, i do enable one textbox so i want to validate that textbox as well if checkbox is checked (textbox shouldn't be empty if checkbox.checked==true). and same with radio buttons.

Thanks in advance
0
irfan_gc
Asked:
irfan_gc
  • 5
  • 4
1 Solution
 
sukumar_diyaCommented:
Hi,

the below js function will do the work for u. register thebelow function to onclick event of the check box.
like ...
<asp:CheckBox id="CheckBox1" onclick="checkbox1_click(this);" style="Z-INDEX: 104; LEFT: 144px; POSITION: absolute; TOP: 56px"
                        runat="server"></asp:CheckBox>

and paste the below function inside script block. Please changethe ctrl names.

function checkbox1_click(chk)
            {
                  
                  var TextBox1 = document.getElementById("TextBox1");
                  var RequiredFieldValidator1 = document.getElementById("RequiredFieldValidator1");
                  if(chk.checked == true)
                  {
                        TextBox1.disabled = false;
                        RequiredFieldValidator1.enabled = true;
                  }
                  else
                  {
                        TextBox1.value = "";
                        TextBox1.disabled = true;
                        RequiredFieldValidator1.enabled = false;
                        RequiredFieldValidator1.style.display = "none";
                  }
            
            }

Hope this helps you,
Suku
0
 
irfan_gcAuthor Commented:
Sorry I forgot to mension that I'm using C# language and also I dont want to use AutoPostBack=true property on selection change or checkbox clicked.
0
 
sukumar_diyaCommented:
hi Irfan,
please try the code in my prev msg and u can set the autopostback =false. Its fully client side JS function. There is no postback in it.

Suk
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
irfan_gcAuthor Commented:
Hi Suk, there are many errors while building or I may not understanding correctly. here is the code

<script runat="server">
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
          {
               
               TextBox txtOther = document.getElementById("txtOther");
               RequiredFieldValidator RequiredFieldValidator2 = document.getElementById("RequiredFieldValidator2");
               if(CheckBox1.Checked == true)
               {
                    txtOther.Enabled = true;
                    RequiredFieldValidator2.Enabled = true;
               }
               else
               {
                    txtOther.text = "";
                    txtOther.Enabled = false;
                    RequiredFieldValidator2.Enabled = false;
                    RequiredFieldValidator2.Style.Display = "none";
               }
          }    
   
</script>

and in body

<asp:CheckBox ID="CheckBox1" AutoPostBack=false runat="server" Text="Other(s)" OnCheckedChanged="protected void CheckBox1_CheckedChanged" />
<asp:TextBox ID="txtOther" runat="server" Enabled =false></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* missing" ControlToValidate="txtOther" Enabled=false ></asp:RequiredFieldValidator><br />

there are some properties which are not available in c# like OnClick="method to be caled" in CheckBox control.

thanks
0
 
irfan_gcAuthor Commented:
OnCheckedChanged="protected void CheckBox1_CheckedChanged"

sorry it should be like

OnCheckedChanged="CheckBox1_CheckedChanged"

also i wonder if "var" and "document" works in c#
0
 
sukumar_diyaCommented:
Dude,
The function is a Javascript function and it works in client side. No need to post back
Please set the autopostback=false to check box. and

<script language=javascript>
function checkbox1_click(chk)
          {
               
               var txtOther = document.getElementById("txtOther");
               var RequiredFieldValidator2 = document.getElementById("RequiredFieldValidator2");
               if(chk.checked == true)
               {
                    txtOther.disabled = false;
                    RequiredFieldValidator2.enabled = true;
               }
               else
               {
                    txtOther.value = "";
                    txtOther.disabled = true;
                    RequiredFieldValidator2.enabled = false;
                    RequiredFieldValidator2.style.display = "none";
               }
         
          }

</script>

Paste above code inside head section of the HTML.


in page_load of the code behind paste this code..
CheckBox1.Attributes.Add("onclick","checkbox1_click(this);");

Try this and tell me ..

Suk
0
 
irfan_gcAuthor Commented:
:) sorry. yes it works but validator works for only once when page loads. its because
CheckBox1.Attributes.Add("onclick", "checkbox1_click(this);"); is in form load.

also how can i use this method for two radio buttons selection without invoking AutoPostBack?

Thanks alot!
0
 
sukumar_diyaCommented:
Hi irfan,
For both controls add the attribute in onclick. I hope it will work..
If doesn't work tell me i will try ..

Suk
0
 
irfan_gcAuthor Commented:
Everything is working now. just last problem:

protected void Page_Load(object sender, EventArgs e)
    {

        CheckBox1.Attributes.Add("onclick", "checkbox1_click(this);");
        Radio1.Attributes.Add("onclick", "radio1_selection(this);");
        Radio2.Attributes.Add("onclick", "radio2_selection(this);");
    }

I am adding attributes on form load only. so if you change the selections several times and then click "submit", validators for these controls dont work. Can I add attribute somewhere else?

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now