0

Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.

I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.

<asp:Button ID="Button3" runat="server" Height="24px" 
        Text="Save" Visible="False" Width="67px" Font-Bold="True" 

  causesvalidation="true"
  validationgroup="required"
  runat="Server" />


    <asp:CheckBox ID="CheckBox1" runat="server" 
        oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent" 
        ValidationGroup="required" AutoPostBack="True" Enabled="False" />
0

3 Answers 3

2

You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

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

Comments

0

I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.

6 Comments

I agree. with you I just don't understand how I cna control different validation groups under one button
It would be better to say I would never only do form validation in JavaScript, otherwise you miss a great opportunity to make your app better for the users.
And believe it or not, some people deliberately do things with your server you don't want them to. I will almost always do validation in javascript - but that's to be nice to honest mistakes by correcting them faster, and for that reason alone. The real validation happens on the server.
The ValidationGroup attribute is used to restrict which validators are activated on a postback from a given button. It is not used to group controls for combined validation. The CustomValidator will allow you to write your own validation logic, for both the server and the client as a good developer should, so you can choose only to validate the TextBox entry if the CheckBox is checked.
that sounds what I want to do, I want to validate that a textbox has an entry if a checkbox is checked on button click. just not sure how to do this as I tried with validategroup but it seems as though only one button can control one validation group i have many validation groups that i want controlled by one button
|
0

you can use validation with Ajax (in Ajax postback occures but you will not sense)

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.