Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Restricting the textbox to enter number only using thier ASCII codes

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 506
    Comment on it

    We can disable the keys to enter inside the textbox. For example if we have a textbox for entering user's age then a user only need to enter numbers (The age will only be in number format ie 12,18 and so on)

    We can use the following javascript to disable rest of the keys inside the textbox using the ASCII codes of the characters:-

    function isNumberKey(evt) {
                var charCode = (evt.which) ? evt.which : event.keyCode
                if (charCode > 31 && (charCode < 48 || charCode > 57))
                    return false;
    
                return true;
            }
    

    It will pick up the ASCII which lies only between 48 to 57(0-9). We can call them on textbox by the following way:-

    <input name="txtNumbers" type="text"  id="txtNumbers" runat="server" onkeypress="return isNumberKey(event)" placeholder="Enter numbers only" />
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: