Introduction:
In this article I explain to create validations for Text Box
control in ASP.Net. Here I am going to start with three validations:
1.
Accept alphanumeric
characters
2.
Accept Alphabet characters
3.
Accept numeric characters
Requirment :
Add the below line in
head section:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
Code:
For the point
first write the below script in your page :
1. Accept alphanumeric
characters :
<script type="text/javascript">
$(function () {
$('#txtalphaNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >=
35 && key <= 40) || (key >= 65 && key <= 90) || (key
>= 48 && key <= 57) || (key >= 96 && key <= 105)))
{
e.preventDefault();
}
}
});
});
When you have done with this
code, add a textbox and mention the id of textbox to “txtalphaNumeric” like :
<asp:TextBox ID="txtalphaNumeric" runat="server"></asp:TextBox>
2. Accept Alphabet characters
For the point first write the below
script in your page :
$(function () {
$('#txtalphabet').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >=
35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
});
<asp:TextBox ID="txtalphabet" runat="server"></asp:TextBox>
3. Accept numeric characters :
For the point first write the below
script in your page :
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 &&
key <= 40) || (key >= 48 && key <= 57) || (key >= 96
&& key <= 105))) {
e.preventDefault();
}
}
});
});
When you have done with this
code, add a textbox and mention the id of textbox to “txtalphabet” like :
<asp:TextBox ID="txtNumeric" runat="server"></asp:TextBox>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Allow only alphanumeric characters in textbox</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#txtalphaNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >=
35 && key <= 40) || (key >= 65 && key <= 90) || (key
>= 48 && key <= 57) || (key >= 96 && key <= 105)))
{
e.preventDefault();
}
}
});
});
//only
alphabet
$(function () {
$('#txtalphabet').keydown(function (e) {
if
(e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >=
35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
});
//Only
Numeric
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 &&
key <= 40) || (key >= 48 && key <= 57) || (key >= 96
&& key <= 105))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Enter Text:</b><asp:TextBox ID="txtalphaNumeric" runat="server"></asp:TextBox>Accept alphanumeric
characters<b><br />
Enter Text:</b><asp:TextBox ID="txtalphabet" runat="server"></asp:TextBox>
Accept only Alphabets characters
Enter text :<asp:TextBox ID="txtNumeric" runat="server"></asp:TextBox>Accept only Numeric values
</div>
</form>
</body>
</html>
Save all and view the page in browser it ll work fine .
Thanks
No comments:
Post a Comment