Posted by : Anwar Hossain
Category : How to use different types of validation control using asp.net c#

How to use Custom Validator control using asp.net c#

Dear viewers I will show how to use asp.net client side custom Validator Control. First example an error Message will display when the length of text input in the text box exceeds more than 25. Second example two drop down list are used to check start date and end date.

Custom Validors Example ASP.NET C#

 

HTML



<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
  width: 100%;
}
.style2
{
  color: #FF9900;
}
.style3
{
  color: Teal;
}
</style>
<script language="javascript">
 
function validateLength(sender, args) {
  if (args.Value.length <= 20)
  args.IsValid = true;
  else
  args.IsValid = false;
}
 
 
</script>
<script type="text/javascript" language="javascript">
function CustomValidate(sender, args) {
  if (document.getElementById('drpAdmission').selectedIndex > document.getElementById('drpPassingdate').selectedIndex) {
  args.IsValid = false;
  }
  else {
  args.IsValid = true;
  }
}
 
</script>
</head>
<body>
<form id="form1" runat="server">
<span class="style3"><b style="font-weight: bold; font-size: x-large">Client Side Custom
Validation First example </b></span>&nbsp;<table class="style1">
  <tr>
  <td width="150">
  &nbsp;
  </td>
  <td width="5px">
  &nbsp;
  </td>
  <td class="style2">
  </td>
  </tr>
  <tr>
  <td width="150" align="char">
  <strong>Enter text to test </strong>
  </td>
  <td width="5px">
  :
  </td>
  <td>
  <asp:TextBox ID="txtParagraph" runat="server" Height="50px" TextMode="MultiLine"
  Width="250px"></asp:TextBox>
  </td>
  </tr>
  <tr>
  <td>
  &nbsp;
  </td>
  <td>
  &nbsp;&nbsp;
  </td>
  <td>
  <asp:Button runat="server" ID="btnSubmit" Text="Submit" CausesValidation="true" />
  <asp:CustomValidator runat="server" ID="CVParagrph" ControlToValidate="txtParagraph"
  ForeColor="Red" ClientValidationFunction="validateLength" Text="Only 25 Cahracter is allowed to input">
  </asp:CustomValidator>
  </td>
  </tr>
</table>
<span class="style3"><b style="font-weight: bold; font-size: x-large">Client Side Custom
Validation Second example</b></span>
<table class="style1">
<tr>
  <td width="150">
  &nbsp;
  </td>
  <td width="5px">
  &nbsp;&nbsp;
  </td>
  <td class="style2">
  </td>
</tr>
<tr>
  <td width="150">
  <strong>Admission&nbsp; Date </strong>
  </td>
  <td width="5px">
  &nbsp;:&nbsp;
  </td>
  <td>
  <asp:DropDownList ID="drpAdmission" runat="server" Width="100px" ValidationGroup="year">
  </asp:DropDownList>
  </td>
</tr>
<tr>
  <td width="150">
  <strong>Passing Year</strong>
  </td>
  <td width="5px">
  &nbsp;:&nbsp;
  </td>
  <td>
  <asp:DropDownList ID="drpPassingdate" runat="server" Width="100px" ValidationGroup="year">
  </asp:DropDownList>
  </td>
</tr>
<tr>
  <td width="150">
  &nbsp;
  </td>
  <td width="5px">
  &nbsp;
  </td>
  <td>
  &nbsp;
  <asp:CustomValidator ID="custvaddropdown" runat="server" ValidationGroup="year" ClientValidationFunction="CustomValidate"
  ControlToValidate="drpAdmission" ErrorMessage="Admission date must be less then passing date"
  Text="Admission date must be less then passing date." ForeColor="red"></asp:CustomValidator>
  </td>
</tr>
<tr>
  <td>
  &nbsp;
  </td>
  <td>
  &nbsp;&nbsp;
  </td>
  <td>
  <asp:Button ID="btntest" runat="server" Text="Year Test" ValidationGroup="year" />
  </td>
</tr>
</table>
</form>
</body>
</html>

CODE BEHIND



using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class TestDesign_CustomValidator : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
 
  LoadYear();
 
  }
  }
 
 
 
  private void LoadYear()
  {
  for (int i = 1985; i < 2016; i++ )
  {
 
  drpAdmission.Items.Add(i.ToString());
  drpPassingdate.Items.Add(i.ToString());
 
  }
 
  }
}
 

Out Put

custome_validator



Realted Article Headline

How to use RequiredFieldValidator for RadioButtonList in ASP.NET C#
How to use RequiredFieldValidator for DropDownList in ASP.NET C#
How to use RangeValidator in ASP.NET C#
how to use RequiredFieldValidator in ASP.NET C#
How to use RegularExpressionValidator Control in ASP.NET using C#
How to use Custom Validator control using asp.net c#
How to use compare validation control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection