Reputation: 13
I have a table with about 30 textboxes.
Each row has 2 textboxes that have the same prefix but different suffix such as txtAvg
From
and txtAvg
To
.
I need to loop through the table and perform custom validation on the textboxes:
I might need more validations later on. Also, if the to textbox is filled in then the from isn't required and vice versa. No textbox is required.
I need to do server side validation.
Should I add custom validation control? If yes, then for each textbox?
Do I set controltovalidate
?
I need to display the correct error next to each row when submit button is clicked. And obviously if page is not valid then don't do anything further.
I don't want to add different validation controls. Only a custom validator. I also need to do it dynamically and when the user clicks the submit button I want validation to occur and show (*) next to the row with the error and show the complete error message in validation summary.
I tried to add the control but it doesn't get fired when button is clicked.
Private Sub AddCustomValidation(ByVal container As Control)
Dim custom As CustomValidator
Dim vldtxt As TextBox
For Each ctrl In container.Controls
If TypeOf ctrl Is TextBox Then
vldtxt = CType(ctrl, TextBox)
custom = New CustomValidator
custom.Text = "To field must be bigger than from field"
custom.ControlToValidate = vldtxt.ID
AddHandler custom.ServerValidate, AddressOf CustomValidation
' custom.ServerValidate += New System.Web.UI.WebControls.ServerValidateEventHandler(customvalidation)
Me.form1.Controls.Add(custom)
Else
AddCustomValidation(ctrl)
End If
Next
End Sub
Protected Sub CustomValidation(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim vldtxt As TextBox
Dim othertxt As TextBox
Dim othertxtid As String
Dim txtid As String
Dim lower As String
Dim higher As String
For Each ctrl As Control In tbl1.Controls
vldtxt = TryCast(ctrl, TextBox)
othertxt = TryCast(ctrl, TextBox)
If vldtxt IsNot Nothing Then
txtid = ctrl.ID
''check if to is not empty and if not empty then check that that to value is higher
''than from value
If ((txtid.ToLower.EndsWith("to")) And (vldtxt.Text <> "")) Then
higher = (vldtxt.Text)
othertxtid = vldtxt.ID.Replace("To", "From")
othertxt = Page.FindControl(othertxtid)
othertxt.ID = othertxtid
lower = (othertxt.Text)
If lower = String.Empty Or higher = String.Empty Then
args.IsValid = True
End If
If Int32.Parse(lower) >= Int32.Parse(higher) Then
args.IsValid = False
End If
Else
args.IsValid = True
End If
End If
Next
End Sub
The layout is something like this:
<table id="tbl1" runat="server" cellpadding="0" cellspacing="3">
<tr>
<td>From price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceFrom">
</td>
<td>To price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceTo">
</td>
<td>From yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldFrom">
</td>
<td>To yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldTo">
</td>
</tr>
</table>
Upvotes: 1
Views: 1044
Reputation: 6852
You can use ASP.NET Validation Controls:
CustomValidator (validate in code behind)
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="if both to and from textboxes are filled in I have to check that the value in from textbox is smaller than the from value" >
</asp:CustomValidator>
<table>
<tr>
<td>From price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceFrom"></asp:TextBox>
<asp:RegularExpressionValidator runat="server"
ControlToValidate="txtPriceFrom"
ErrorMessage="Please Enter Only Numbers"
ForeColor="Red"
ValidationExpression="^\d+$">
</asp:RegularExpressionValidator>
<asp:RangeValidator runat="server"
ControlToValidate="txtPriceFrom"
ErrorMessage="Range entered in each textbox is 1-100"
MaximumValue="100" MinimumValue="1">
</asp:RangeValidator>
</td>
<td>To price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceTo"></asp:TextBox>
...
</td>
<td>From yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldFrom"></asp:TextBox>
...
</td>
<td>To yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldTo"></asp:TextBox>
...
</td>
</tr>
</table>
Upvotes: 1