Step 1:- Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Alphabetic
& Numeric</td>
<td>
<asp:TextBox ID="txtAlphaNumeric" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 2:- Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Default :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
#region Validation
Methods
public static bool IsAlphaNumeric(string strAlphaNumeric)
{
string strPattern
= @"^[a-zA-Z0-9\s,]*$";
Regex objPattern
= new Regex(strPattern, RegexOptions.IgnoreCase);
return objPattern.IsMatch(strAlphaNumeric);
}
#endregion
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtAlphaNumeric.Text))
{
DisplayMessage("Alphabetic
& Numeric field cannot be blank.");
txtAlphaNumeric.Focus();
return;
}
else
{
if (!IsAlphaNumeric(txtAlphaNumeric.Text))
{
DisplayMessage("Please
enter alphabetic numeric character.");
txtAlphaNumeric.Focus();
return;
}
}
}
#region DisplayMessage
protected void DisplayMessage(string strMsg)
{
strMsg
= strMsg.Replace("\r\n", "\\n");
strMsg
= strMsg.Replace("\n", "\\n");
strMsg
= strMsg.Replace("'", "");
string scriptString
= @"<script
language=""JavaScript"">";
scriptString
+= "alert('" + strMsg + "');";
scriptString
+= "</script>";
ClientScript.RegisterStartupScript(this.GetType(), "Startup",
scriptString);
}
#endregion
}
}
No comments:
Post a Comment