?
Solved

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

Posted on 2006-05-14
9
Medium Priority
?
382 Views
Last Modified: 2012-05-05
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
Comment
Question by:irfan_gc
  • 5
  • 4
9 Comments
 
LVL 7

Expert Comment

by:sukumar_diya
ID: 16677790
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
 

Author Comment

by:irfan_gc
ID: 16677995
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
 
LVL 7

Expert Comment

by:sukumar_diya
ID: 16678024
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:irfan_gc
ID: 16678334
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
 

Author Comment

by:irfan_gc
ID: 16678342
OnCheckedChanged="protected void CheckBox1_CheckedChanged"

sorry it should be like

OnCheckedChanged="CheckBox1_CheckedChanged"

also i wonder if "var" and "document" works in c#
0
 
LVL 7

Accepted Solution

by:
sukumar_diya earned 200 total points
ID: 16679488
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
 

Author Comment

by:irfan_gc
ID: 16679536
:) 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
 
LVL 7

Expert Comment

by:sukumar_diya
ID: 16680072
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
 

Author Comment

by:irfan_gc
ID: 16684663
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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