Mega Code Archive

 
Categories / ASP.Net / Validation By Control
 

Define your own logic with CustomValidator

<%@ Page Language="C#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server" language="C#"> protected void ServerAddMoreValidation(object o, ServerValidateEventArgs e) {     try {         int theInput = Int32.Parse(e.Value);         if (theInput < 0) {             theValidator.ErrorMessage = "please enter a positive value";         }         else {             theValidator.ErrorMessage = "please enter at least " + (theInput + 1).ToString();         }     }     catch {     }     e.IsValid = false; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Target Page</title> </head> <body> <form runat="server"> Enter the quantity: <asp:textbox runat="server" id="quantity" /> <asp:CustomValidator      runat="server"     id="theValidator"      ControlToValidate="quantity"     OnServerValidate="ServerAddMoreValidation"      ErrorMessage="Try Again"/><br /> <asp:label runat="server" id="theFeedback"/><br /> <asp:button type="submit" runat="server" Text="Submit" /> </form> </body> </html>