0

In am using htm required="required" validation but it is not working properly as needed. I have 2 button on single page and I want that on 1 button click it validate for textbox1 and on click of 2nd button it will validate for textbox2.

Please review attached image for more detail enter image description here

2 Answers2

0

You are looking for ValidationGroup. By giving a collection of Validators and a Button the same id, the corresponding button will only validate those fields.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>

<asp:Button ID="Button1" runat="server" Text="Button Group 1" ValidationGroup="group1" />


<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>

<asp:Button ID="Button2" runat="server" Text="Button Group 2" ValidationGroup="group2" />
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • I had applied validation group but it is not working of html required validation –  Mar 04 '17 at 11:37
  • Hey i want to apply it on html validator and not our asp.net required field validator –  Mar 04 '17 at 11:53
  • I you don't want to do anything server-side, then you'll need to use JavaScript. Can you program in JavaScript? – Rolf Mar 04 '17 at 12:46
  • @Rolf don't u think this process will become lenthy, do we have any short way using html Validator –  Mar 04 '17 at 12:55
  • The small snippet from [w3schools](https://www.w3schools.com/tags/att_input_required.asp) does work with asp.net (just tested it). But since it's html5 it only works with the latest browsers. Since older browsers do not work you will still need classic validation. – VDWWD Mar 04 '17 at 14:20
  • @Xtremcool I can't have an opinion on that because it's all relative, it depends what lengthy means to you in this context, and it also depends how skilled you are in JavaScript, and finally I don't have a clear idea of the solution so I don't really know! I have done a quick research and added an (somewhat incomplete) answer below. – Rolf Mar 04 '17 at 14:30
0

I don't think HTML does that natively (grouped validation).
If server-side is not an option, then you'll need to use some JavaScript.
I have found some inspiration in this answer for a related question (Triggering HTML5 Form Validation) : https://stackoverflow.com/a/39689115/370786

If you group each set of inputs that you want to validate together in a form (so you will have several forms), then you can trigger validation through Javascript. Then you can stop the form from posting. This can be done through Javascript - maybe it can be done in HTML but I'm not sure.

Then once everything has validated you will need to post the data to the server, using some Javascript. It might be as simple as just triggering the submit() method on a form element.

Community
  • 1
  • 1
Rolf
  • 5,550
  • 5
  • 41
  • 61