Skip to content

Instantly share code, notes, and snippets.

@hakobyansen
Last active May 20, 2019 16:21
Show Gist options
  • Select an option

  • Save hakobyansen/e42fa121038171bc17fcaaa336142579 to your computer and use it in GitHub Desktop.

Select an option

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.
<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