Last active
May 20, 2019 16:21
-
-
Save hakobyansen/e42fa121038171bc17fcaaa336142579 to your computer and use it in GitHub Desktop.
An example of how to validate maxlength attribute for input type="number" and how to do not allow to put "e" symbol.
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
| <input type="number" | |
| oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" | |
| maxlength="6" required/> | |
| <script> | |
| // do not allow to put "e" symbol | |
| $('input[type="number"]').on('keydown', function(e) { | |
| let keyCode = e.keyCode || e.which; | |
| if( keyCode == 69 ) | |
| { | |
| return; | |
| } | |
| }); | |
| // or | |
| $('input[type="number"]').on('keydown', function (e) { | |
| let value = String.fromCharCode(e.keyCode); | |
| if( value === 'E' ) | |
| { | |
| e.preventDefault(); | |
| } | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment