In this example, we will simply check the length of the string in the
TextBox. This is a very basic and that useful example, only made to show
you how you may use the CustomValidator.
As you can see, it's pretty simple. The only unknown property is the onservervalidate event. It's used to reference a method from CodeBehind which will handle the validation. Switch to our CodeBehind file and add the following method:
Custom text: <asp:textbox id="txtCustom" runat="server"> <asp:customvalidator controltovalidate="txtCustom" errormessage="The text must be exactly 8 characters long!" id="cusCustom" onservervalidate="cusCustom_ServerValidate" runat="server"> </asp:customvalidator></asp:textbox>
As you can see, it's pretty simple. The only unknown property is the onservervalidate event. It's used to reference a method from CodeBehind which will handle the validation. Switch to our CodeBehind file and add the following method:
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e) { if(e.Value.Length == 8) e.IsValid = true; else e.IsValid = false; }This is very simple. The validator basically works by setting the e.IsValid boolean value to either true or false. Here we check the e.Value, which is the string of the control being validated, for it's length. If it's exactly 8 characters long, we return true, otherwise we return false.
No comments:
Post a Comment