Created
          September 20, 2013 13:51 
        
      - 
      
- 
        Save samatsav/6637884 to your computer and use it in GitHub Desktop. 
    Allow only numeric input inside text field
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | document.getElementsByTagName('input')[0].onkeypress = function(e) { | |
| e = e || event | |
| var chr = getChar(e) | |
| if (!isNumeric(chr) && chr !== null) { | |
| return false | |
| } | |
| } | |
| // helper functions | |
| function isNumeric(val) { | |
| return val !== "NaN" && (+val)+'' === val + '' | |
| } | |
| function getChar(event) { | |
| if (event.which == null) { | |
| return String.fromCharCode(event.keyCode) // IE | |
| } else if (event.which!=0 && event.charCode!=0) { | |
| return String.fromCharCode(event.which) // the rest | |
| } else { | |
| return null // special key | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment