In this article I will show you how you can restrict user to
allow only enter alphabet in a text box using javascript in your asp.net application.
Some of my previous asp.net articles are as follows:
Display
No of Characters Entered In TextArea Using jQuery, How
to Create and Read Cookies In ASP.NET Using C#, How
to get a User's Client IP address in ASP.NET using C#.Net, Drag
Drop Cells in GridView Control Using Asp.net C# and jQuery,
JavaScript
Confirm Message From Code Behind in Asp.Net Using C#, How
to call javascript function from code behind Using C# in Asp.Net.
So for this article first we will create a new asp.net
application add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AllowCharactorOnly.aspx.cs"
Inherits="ProjectDemo_Asp.et.AllowCharactorOnly" %>
<!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>Allow
Only Alphabets in TextBox Using Javascript in Asp.Net</title>
<script language="javascript">
function
onlyAlphabets() {
var
regex = /^[a-zA-Z]*$/;
if
(regex.test(document.getElementById("txtdetail").value))
{
return
true;
} else
{
alert("Alphabets Only Please.");
return
false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtdetail" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClientClick="javascript:return onlyAlphabets();" onclick="Button1_Click"/>
<br />
<asp:Label ID="lblmessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
|
I have added code to verify the pages have been submitted.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class AllowCharactorOnly : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
lblmessage.Text = "Form submitted.";
}
}
}
|
Now run the application
DOWNLOAD
0 comments:
Please let me know your view