I have a project where I want to get numbers only as input so I'm using a input of type number but this also allows decimal seperators etc. how would I prevent the use of ,
.
and e
.
I have looked around and couldn't find one that gave the desired result, sinds they either didn't block the decimal inputs, or would allow a user to still paste it in the field.
I have javascript and jquery that I can use, but I do not want to use an external librairy.
If anyone would be able to help me that would be appriciated.
function LimitKeys($id) {
$field = document.getElementById($id);
$value = $($field).val(); //if the value contains multiple e , or . the value is no longer valid and gives me blank
console.log($value);
$regex = /[^\d]/;
$val = $value.replace($regex, "");// cant replace in blank
console.log($val);
//$($field).val($val);
}
$('.number-only').keypress(function (event) { //sinds it's on keypress it won't catch the paste of data.
console.log(this.value);
var regex = new RegExp("^[0-9]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});