Monday, November 22, 2010 string compare validator for case-insensitivity & textbox trim

We have asp:CompareValidator to validate two elements of type(string, integer, double etc). While comparing two strings, it considers text case-sensitivity. Some times we may need to compare strings without taking care of its case.
I got the same requirement while working in a project, where i need to compare two email addresses.

I have written StringCompareValidator class which extends BaseValidator class, it serves above the need. It does client and server side validations. Use its properties "IgnoreCase", "TrimText", "Operator" and "EnableClientScript" to change the behavior of the validator.

Download its sample website from here. StringCompareValidator.cs class file from here.


Place StringCompareValidator class file in App_Code folder and register this control in a page as.
<%@ Register Assembly="App_Code" Namespace="RampValidators" TagPrefix="RampValidators" %>
Using the above validator for textbox controls tbEmail1, tbEmail2 as
<RampValidators:StringCompareValidator ID="StringCompareValidatorReEmail" ControlToValidate="tbEmail2" ControlToCompare="tbEmail1" Text="Email addresses should match." Display="Dynamic" TrimText="false" IgnoreCase="true" Operator="Equal" runat="server" CssClass="required"></RampValidators:StringCompareValidator>