Skip to content

Instantly share code, notes, and snippets.

@vishvendrasingh
Last active September 18, 2018 06:16
Show Gist options
  • Save vishvendrasingh/2d13a2ea55c49c4fd2ac09f1cfe90126 to your computer and use it in GitHub Desktop.
Save vishvendrasingh/2d13a2ea55c49c4fd2ac09f1cfe90126 to your computer and use it in GitHub Desktop.
input character or number only using javascript
##character Only
<input type="text" name="input_name" onkeypress='return ((event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode == 32))'>
##Number Only
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
##Check charCode
"+".charCodeAt()
<script>
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode
return !(charCode > 31 && (charCode < 48 || charCode > 57));
}
function alphaOnly(event) {
var key = event.keyCode;
return ((key >= 65 && key <= 90) || (event.keyCode > 96 && event.keyCode < 123) || key == 8);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment