Page

Asp.net validation controls and server side validation


// Default.aspx //


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!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 runat="server">
    <title>validation controls</title>
</head>
<body bgcolor="Maroon">
    <form id="form1" runat="server">
    <div>
Enter Number:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ForeColor="Red" ErrorMessage="Please enter numeric values."
    OnServerValidate="Validate_Numeric" ControlToValidate="TextBox1"></asp:CustomValidator>
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

   
    </div>
    </form>
</body>
</html>


// Default.aspx.cs //


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
    {
    }

protected void Validate_Numeric(object sender, ServerValidateEventArgs e)
{
System.Text.RegularExpressions.Regex r= new System.Text.RegularExpressions.Regex("^[0-9]+$");
    e.IsValid = r.IsMatch(TextBox1.Text);
}
}

OUTPUT


No comments:

Post a Comment