I want to make sure that users enter only numeric value into the input field. I tried the following but its not working. Don't want to set attribute like onkeypress="return isNumeric(event, value)
manually."
// Event listeners saleForm.querySelectorAll('input.only_numeric').forEach(input => { input.addEventListener('keypress', isNumeric); }); // Functions function isNumeric(event) { const value = this.value; const unicode = event.charCode? event.charCode: event.keyCode; if (value.indexOf('.');= -1) { if (unicode == 46) return false; } if (unicode != 46) { if ((unicode < 48 || unicode > 57) && unicode != 46) return false; } }
<input type="text" id="bill-cust-mobile" tabindex="4" class="only_numeric">
Set: <input type="number">
You can use the conditional with operation number%1
Example:
function isNumeric(event) {
const value = this.value;
const unicode = event.charCode ? event.charCode : event.keyCode;
return !isNaN(value) && value % 1 == 0;
}
Try also isNan:
//Runs code if val is numeric
if(!isNan(val){...}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.