how to validate textbox not to enter numbers in textbox

Posted by Shanky11 under JavaScript on 9/23/2013 | Points: 10 | Views : 1159 | Status : [Member] | Replies : 4
function checkNum()
{

if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
return true;
else
{
alert("Please enter only char");
return false;
}

}
<asp:TextBox ID="txtcity" runat="server" onkeypress=" return checkNum();"></asp:TextBox>
Why its not working??????????????????




Responses

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
void NotNumberValidation(object sender, KeyPressEventArgs e)

{
if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
return true;
else {
alert("Please enter only char");
return false;
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
The code is also working for me.....................
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NotToEnterNumerics.aspx.cs" Inherits="TextBoxValidation.NotToEnterNumerics" %>


<!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></title>
<script type="text/javascript">
function checkNum() {

if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
return true;
else {
alert("Please enter only char");
return false;
}

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtcity" runat="server" onkeypress=" return checkNum();"></asp:TextBox>
</div>
</form>
</body>
</html>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
--Alternate is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NotToEnterNumerics.aspx.cs" Inherits="TextBoxValidation.NotToEnterNumerics" %>


<!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></title>
<script type="text/javascript">
function validate(key) {
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
//comparing pressed keycodes
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return true;
}
else {
alert("Please enter only char");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtcity" runat="server" onkeypress="return validate(event);"></asp:TextBox>
</div>
</form>
</body>
</html>


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Click on "Mark As Answer" ( The above codes are working in my system)

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response