Angular | Typescript Syntax
HTML:
<input type="number" (keydown)="onKeyDown($event)">
ts File:
onKeyDown(e:any):void{
if(!e)
return;
console.log('ee',e);
if((e.code==='Minus' && e.keyCode==189 && e.key==='-') || (e.keyCode==187 && e.key==='+')
||(e.code==='KeyE' && e.keyCode==69 && e.key==='e')){
e.preventDefault();
}
}
JavaScript Syntax
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1>Create Account</h1>
<form id="user" name="user">
<div class="row">
<div class="col-6">
<label for="fname">First Name*</label>
<input type="number" id="fname" placeholder="Enter Your first name" required>
</div>
</div>
</form>
</div>
</body>
</html>
<script>
$('#fname').keydown(function(e){
console.log('evt e.key:::::', e);
if(e.key=='-' && e.keyCode==189 || e.key=='+' && e.keyCode==187 )
{
e.preventDefault();
}
console.log('evt e.keyCode:::::', e.keyCode);
console.log('evt e.code:::::', e.code);
});
</script>
pattern="[1-9][0-9]*"
. Change the[1-9]
to[1-9]?
if you don't require any input. – Bedouin