How to restrict user to enter only digits(numbers) in textbox using javascript


<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
<script type=”text/javascript”>
function allowordeny(key) {
var datakey = (key.which) ? key.which : key.keyCode;//capturing pressed key
var textboxdata = document.getElementById(“TextBox1”);//capturing textbox reference
if (datakey == 46)//allowing delete key
return true;
else if (datakey == 8)//allowing backspace key
return true;
else if ((datakey < 48 || datakey > 57))//avoiding charecters other than digits
return false;
else if (textboxdata.value.length < 10)//restricting morethan 10 digits
return true;
else {
alert(“Allow Only 10 digits”);//giving message
return false;
}
}
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”TextBox1″ onkeypress=”return allowordeny(event)”
runat=”server”></asp:TextBox>
</div>
</form>
</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s