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>
1 comments:
commentsThis code will not work with mobile
ReplyThanks for comments.....