Define your own logic with CustomValidator : CustomValidator « Validation by Control « ASP.Net






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>

 








Related examples in the same category

1.CustomValidator: value length less than 100 (C#)
2.Customvalidator Demo (VB.net)
3.asp:CustomValidator validation (VB.net)
4.asp CustomValidator: validate event function (C#)
5.Client side and server side validation