How to implement Custom Validator in ASP.NET

lakhansin-22735
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 3927 red flag

In this article we will look into how to use ‘ASP.NET Custom Validator’ in a very simpler way and this will be very helpful to those who has never used Custom Validator in their projects.


 Download source code for How to implement Custom Validator in ASP.NET

Introduction

Firstly, this article is for beginners, in this article we will see how to use ‘ASP.NET validation Custom Validator’ in a very simpler way and this will be very helpful to those who has never used custom validator in their projects. I’m sure after reading my article, you will be in that situation where you can easily go with custom validator control. Please give your suggestions and feedback in the comments.

Why we choose Custom Validator ?

If anyone of the other ASP.NET validation controls do not suit your requirements, then you can define a custom server-side validation function and call it using Custom Validator control.

How to implement Custom Validator in ASP.NET project?


See the below ASP.NET inline code

<asp:TextBox ID="txtLicenseNo" runat="server" MaxLength="10" Font-Bold="true"></asp:TextBox>
<br />

<asp:RequiredFieldValidator ID="rfvLicenseNo" runat="server"
ControlToValidate="txtLicenseNo"

ErrorMessage="Required"
Display="Dynamic"
SetFocusOnError="true"
ValidationGroup="vgLicense"

EnableClientScript="true">

</asp:RequiredFieldValidator>

 <asp:CustomValidator ID="cmvalLicneseNumber"
SetFocusOnError="true"

ValidationGroup="vgLicense"

ControlToValidate="txtLicenseNo"

ClientValidationFunction="ValLicenseLength"                       
Display="Static"

runat="server"

ErrorMessage="License number should have at least 4 characters!">

</asp:CustomValidator>

<script type="text/javascript">

function ValLicenseLength(sender, e) {

var LicenseLength = (document.getElementById('<%=txtLicenseNo.ClientID %>').value).length;

e.IsValid = true;

if (LicenseLength < 4) {

e.IsValid = false;

}

else {

e.IsValid = true;

}

}

</script>

In above example there, is one TextBox, one RequiredFieldValidator and one CustomValidator whereas RequiredFieldValidator is used to make sure the textbox having text or not and by using CustomValidator we are going to make sure the entered text would be more than of 4 characters. 


CustomValidator having same properties as other Validators except ‘ClientValidationFunction’. This property having JavaScript method which is defined by us. Just give a look.

Review the attached project for more and better understanding.


Conclusion


This is the initial level study of this control but you can elaborate your knowledge as much as you want. We have covered just a small portion. Hope this will help all beginners to get familiar with custom validator. Please give your feedback and suggestions.

Reference


To explore more and more, visit this link
http://msdn.microsoft.com/en-us/library/f5db6z8k%28v=vs.71%29.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

lakhansin-22735
Full Name: Lakhan Singh
Member Level: Starter
Member Status: Member
Member Since: 6/21/2013 3:50:19 AM
Country: India
Lakhan Singh Tech Lead BeyondKey System Pvt. Ltd. Indore, M.P. India
http://www.dotnetfunda.com
Technical Lead

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)