|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionASP.NET validators are a great time saver. However, they lack the built in ability to validate multiple controls on the client side. Luckily, this is easily fixed. BackgroundIt is fairly easy to use something like: <asp:RequiredFieldValidator id=PhoneRequired runat="server"
Display="Dynamic" ErrorMessage="*" ControlToValidate="Phone" />
This requires that the user fills in the Phone field. What if you have a Phone field and an Email field. You could require both fields, but you might want to allow the user to enter only one of these fields. In this case, you couldn't use ExampleRequires that user enters Phone or Email. Client side validation updates when either field is changed. SolutionUpdate!I have added a user control / class based on Multi-Validator which is extended from Fields and CustomValidator<b>Phone: </b><asp:TextBox id="Phone" runat="server"><br />
<b>Email: </b><asp:TextBox id="Email" runat="server">
<asp:Button id="Submit" Text="Submit" /><br />
<%-- AtLeastOneContact Custom Validator --%>
<asp:CustomValidator id="AtLeastOneContact" runat="server"
ErrorMessage="Phone or Email Required"
Display="Dynamic"
OnServerValidate="AtLeastOneContact_ServerValidate"
ClientValidationFunction="AtLeastOneContact_ClientValidate" />
Note: Do not set Server Side Part' Server Side Validation
Sub AtLeastOneContact_ServerValidate(ByVal source As Object, _
ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
'Requires that either Phone or Email is not empty
If Phone.Text & Email.Text <> "" Then
args.IsValid = true
Else
args.IsValid = false
End If
End Sub
Client Side Part<%-- Client Side Validation --%>
<script type="text/vbscript" language="vbscript">
<!--
Sub AtLeastOneContact_ClientValidate(source, args)
'Requires that either Phone or Email is not empty
If document.getElementById("<%= Phone.ClientID %>").value & _
document.getElementById("<%= Email.ClientID %>").value <> "" Then
args.IsValid = true
Else
args.IsValid = false
End If
End Sub
'-->
</script>
Creating the dependency<%-- This configures the validator to automatically--%>
<%-- update when either of these controls is changed --%>
<script type="text/javascript">
<!--
ValidatorHookupControlID("<%= Phone.ClientID %>",
document.all["<%= AtLeastOneContact.ClientID %>"]);
ValidatorHookupControlID("<%= Email.ClientID %>",
document.all["<%= AtLeastOneContact.ClientID %>"]);
//-->
</script>
This is where the magic happens... This informs the built invalidation subroutines to update validation whenever one of these controls change. You can easily add as many controls as needed to a single validator's dependencies. The Note this code must appear after the automatically added I normally use Other ExamplesYou can use this method to create complex validations. For example, I have a drop down list with various options. One of the options is "other / see comments". The drop down list also includes a blank entry "select option". I created a validator that requires that the user enters a value for the dropdown, and if they select "other / see comments" then it requires that something is in comments. You can validate two drop down lists against each other. For example, if you were selling clothes you could have a dropdown for the type of item and one for color selection. Then in your validator, you could show the error message if that item is not available in that color. That is you sell shirts in blue, green and red. But only sell pants in blue and green. DemoThe zip file contains a single working aspx page. Try it out. Note that validation error messages do not display until you have updated one of the controls or try to submit. You can add History
ThanksIf you read this article then please leave a comment below.
|
||||||||||||||||||||||