allow only number or string in textBox using JavaScript in Asp.Net


Description:-

In this article we will see about Input value in Text box. Here I have created Sample demo example to input only number or character in text box using JavaScript code.

HTML Code:

<div style=" width: 800px; padding: 20px; height: 300px;">
<table>
<tr><td>Accept Only Number : </td>
<td><asp:TextBox ID="txtNumber" runat="server" Font-Size="20px" Width="250px" onkeypress="return CheckIsNumeric(event);"></asp:TextBox></td>
</tr>
<tr><td>Accept Only String Character : </td>
<td><asp:TextBox ID="txtCharacter" runat="server" Font-Size="20px" Width="250px" onkeypress="return CheckIsCharacter(event);"></asp:TextBox></td>
</tr>
</table>
</div>

JavaScript Code:
<script type="text/javascript">
functionCheckIsNumeric(objEvt) {
varcharCode = (objEvt.which) ? objEvt.which :event.keyCode
if (charCode> 31 && (charCode< 48 || charCode> 57)) {
document.getElementById("txtNumber").style.backgroundColor = "#FFB2B2";
returnfalse;
            }
else {
document.getElementById("txtNumber").style.backgroundColor = "#B2D8B2";
returntrue;
            }
        }
functionCheckIsCharacter(objEvt) {
varcharCode = (objEvt.which) ? objEvt.which :event.keyCode
if ((charCode>= 65 &&charCode<= 90) || (charCode>= 97 &&charCode<= 122)) {
document.getElementById("txtCharacter").style.backgroundColor = "#B2D8B2";
returntrue;
            }
else {
document.getElementById("txtCharacter").style.backgroundColor = "#FFB2B2";
returnfalse;
            }
        }
</script>

Related Posts

Previous
Next Post »

1 comments:

comments
Anonymous
January 20, 2017 at 3:19:00 AM GMT+5:30 delete

This code will not work with mobile

Reply
avatar

Thanks for comments.....