Allow Only Numbers In ASP TextBox and Html TextBox Using JAvaScript
22:25
Here I am Explaining asp TextBox and Html TextBox Allow only Digits Using JavaScript Code.
1. Design the aspx page like below.
1. Design the aspx page like below.
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Asp TextBox:
</td>
<td>
<asp:TextBox ID="txtasp" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
HTML TextBox:
</td>
<td>
<input
type="text"
runat="server"
id="txthtml"
/>
</td>
</tr>
</table>
</div>
</form>
</body>
2.Add javascript code to aspx source page head section.
<head runat="server">
<title></title>
<script type="text/javascript"
language="javascript">
// JAVASCRIPT CODE FOR HTML TEXT BOX..
function numeric(e) {
var unicode = e.charCode ? e.charCode : e.keyCode;
if (unicode == 8 || unicode == 9 || (unicode >= 48
&& unicode <= 57)) {
return true;
}
else {
alert('Please Enter Digits Only')
return false;
}
}
// JAVASCRIPT CODE FOR ASP TEXT BOX..
function isNumberKey(event) {
var charCode = (event.which) ? event.which :
event.keyCode
if (charCode > 31 && (charCode < 48 ||
charCode > 57)) {
alert("Pls Enter Digits Only");
return false;
}
return true;
}
</script>
</head>
3.Apply the java script to textboxes like below..
<asp:TextBox ID="txtasp" runat="server" OnKeyPress="return isNumberKey(event);"></asp:TextBox>
<input type="text" runat="server" id="txthtml" onkeypress="return numeric(event); " />
4.then run the application or press F5 and observe the screen..
0 comments :