HI,
I am trying to validate a portion of asp.net form, each portion has it's checkbox and if checkox is ticked the validation for that portion only should take place.
I have added the html as well.
Group1/2 are place holders id.
Here is a sample problem I have:
When I tick the first checkbox(which is associated with group1 only), a group1(only) validation should happen and validation for second group(Group2) should be ignored. but the result is different, I get validation error message for all textboxes from all groups and submission doesn't happen until I do validate textboxes from unticked portion of the form.....
I appreciate if anyone can help I have a very very...very long form with millions of optional validations...(If I may refer it as such).
Many Thanks
<%@ Page Language="
VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true" %>
<%@ Import NameSpace="System.Configuration" %>
<%@ Import NameSpace="System.Web.UI" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import Namespace="System.Text" %>
<script language="
VB" runat="server">
Sub validate(Src As Object, E As EventArgs)
DisableValidators()
If Box1.Checked then
lblmsg.text = "If box 1 do Validation <br>"
EnableValidators(Group1)
End If
If Box2.Checked then
lblmsg.text = "If box2 do Validation <br>"
EnableValidators(Group2)
End If
Page.Validate()
If Page.IsValid then
lblmsg.text = "The page is valid"
End If
End Sub
Function DisableValidators()
lblmsg.text = "Disabling all validator controls on page"
Dim bv as BaseValidator
For Each bv in Page.Validators
bv.Enabled = false
Next
End Function
Function EnableValidators(Control)
Dim c as Control
For Each c in Control
If TypeOf c is RequiredFieldValidator Then
lblmsg.text = "Enabling " & c.ID.ToString()
CType(c, RequiredFieldValidator).Enabled = True
End If
Next
End Function
Sub submit_clicked(Src As Object, E As EventArgs)
response.redirect("test2.aspx?sv=" & Text1.Text &"&st=" & Text2.Text)
End Sub
</script>
<html>
<body>
<form runat="server" >
<asp:label id="lblmsg" runat="server" />
<table border=1>
<tr>
<td>
<asp:CheckBox id="Box1" Text="Validate Group 1" runat="server"/></td>
<td>
<asp:Placeholder id="Group1" runat="server">
Surname:<asp:TextBox id="Text1" runat="server" width="100"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="Text1"
Display="Static"
InitialValue=""
Width="100%"
runat="server">*Please enter a value for surname
</asp:RequiredFieldValidator>
</asp:Placeholder>
</td>
</tr>
<tr>
<td> <asp:CheckBox id="Box2" Text="Validate Group 2" runat="server"/> </td>
<td>
<asp:Placeholder id="Group2" runat="server">
ZIP Code: <asp:TextBox id="Text2" runat="server" width="100"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="Text2"
Display="Static"
InitialValue="" Width="100%" runat="server">Please enter a value for zip code
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
ControlToValidate="Text2"
ValidationExpression="^\d{5}$"
Display="Static"
Font-Name="verdana"
Font-Size="10pt">
Zip code must be 5 numeric digits
</asp:RegularExpressionValidator>
</asp:Placeholder>
</td>
</tr>
</table>
<asp:Button id="Button1" text="Validate" OnClick="validate" runat="server"/>
<input type="submit" id="submitID2" name="submitID"
text="Apply" value="Submit"
onserverclick="submit_clicked" runat="server"
causeValidation="false" />
</form>
</body>
</html>