0

I have a situauation that I need to check on a button click if the user has entered a value into a textbox, but have not checked a checkbox. They need to be advised that they can not enter a value in the textbox without first checking the checkbox. I know I need to use a CustomValidator Control, but other than that I'm lost. Can anyone help me?

2
  • why not have the textbox disabled and when the user checks the checkbox enable the textbox? Commented Mar 18, 2014 at 19:24
  • @SaverioTerracciano It's a conversion of a classic site. All of the functionality has to match that of the old site. I hoped to use a custom validation control, because I'm using a validation summary which looks exaclty like what they are doing in the old site. Commented Mar 18, 2014 at 19:36

4 Answers 4

1

In OnClientClick event you can call a javascript method which will do this validation for you.

This should point you in the right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't really know JavaScript all that well. I was hoping someone would be able to help me with code.
1

I think the easiest way to do so will be enabling/disabling the textbox base on whether the checkbox is checked.

However, if you want to do the check on button_click, maybe just do checks on both the checkbox and textbox? And output error message to a label or something?

if(TextBox1.Text.Trim() != "")
    if(!CheckBox1.Checked)
        label1.Text = "Checkbox needs to be checked";

Or, you can do checks when TextBox1.Text has changed.

private void Textbox1_TextChanged(object sender, EventArgs e)
{
    if(!CheckBox1.Checked)
        label1.Text = "Checkbox needs to be checked";
}

1 Comment

+1 for the "easiest way to do so will be enabling/disabling the textbox based on the checkbox value. Preventing the user from making the mistake in the first place is a better user experience.
0

You can implement this functality using javascript/Jquery.Set the onClientClick property of button as below:

<asp:Button ID="btnSubmit" runat="server" onClientClick="ValidateData();"/>

Below is the jquery code:

function ValidateData()
{
if(!$('#checkboxID').is(':checked') && $.trim($('#textBoxID').val())!='')
{
alert('Please check the checkbox before entering text');
}

}

Comments

0

You can try this...

First you must have a button like this...

<asp:Button ID="btnCheck" runat="server" CssClass="m-btn purple" Text="Check" onclintclick="Validate()"/>

Now we can write the validation script...

<script type="text/javascript" language="javascript">
        function Validate() {

            if(document.getelementid('checkbox1').checked==true)
            {
              if(document.getelementid('Textbox1').value=="")
              {
               //Write your code here..
              }  
            }
            else
            {
               alert('Please check the checkbox..');
            }
        }
  </script>

Please feel free to mark as answer if it satisfies you....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.