Client Size validation with ASP.NET CustomValidator

Posted by Unknown On Monday, January 5, 2015 0 comments

Using JavaScript to custom validate asp.net control.

.ASPX File & JavaScript
function ValidateAge(source, arguments){
    var pattern = new RegExp("^[0-9]{2}$");
    if (!pattern.test(arguments.Value)){
         source.errormessage ="Age must be numeric";
         arguments.IsValid = false;
    }else{
         arguments.IsValid = true;
    }
}

<asp:CustomValidator 
     id="valCustom" 
     ControlToValidate="txbAge"
     ClientValidationFunction="ValidateAge"
     OnServerValidate="ValidateAge"
     ErrorMessage="This can be set from code"
     ForeColor="red" 
     runat="server">
     ValidationGroup="valSave"
</asp:CustomValidator>

0 comments:

Post a Comment