You can use the CustomValidator control to execute your custom client-side and server-side validation routines. You can associate them with the control so that validation is performed automatically. If the validation fails, the Page.IsValid property is set to false, as occurs with any other validation control.
The client-side and server-side validation routines for the CustomValidator are declared similarly. They both take two parameters:
– A reference to the validator
– A custom argument object. The custom argument object provides:
– A Value property that contains the current value of the associated input control (the value you have to validate)
– An IsValid property through which you specify whether the input value is valid.
function CustIDClientValidate(ctl, args)
// the value is a multiple of 7 if the modulus by 7 is 0
args.IsValid=(args.Value%7 == 0);
protected void CustIDServerValidate(object sender, ServerValidateEventArgs args)
args.IsValid = (int.Parse(args.Value)%7 == 0);
// An error is most likely caused by non-numeric data.
args.IsValid = false;
The next code lines show CustomValidator tag that uses these routines:
<asp:TextBox runat=”server” ID=”CustID” />
<asp:CustomValidator runat=”server” ControlToValidate=”CustID ”
ErrorMessage=”ID must be a multiple of 7″ Display=”dynamic”>*