Here’s my input field with label as ID, I want this input field to only accept numbers without changing the type to number.
<label for="id">ID:</label>
<input type="text"
class="form-control form-control-sm"
formControlName="idCardInput"
name="idCard"
id="idCard"
ng-pattern="/^[0-9]*$/" />
>Solution :
Using Javascript, you can use this function whenever you want inside your particular project or website.
function isNumberKey(event){
var charCode = (event.which) ? event.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
<input name="id" type="text" onkeypress="return isNumberKey(event)"/>