Javascript Code Allow Only Numbers and Single Decimal
Javascript Code Allow Only Numbers and Single Decimal to be Input in Input Type = Text
Home
Short:
/* ------------------------------------------- allow only numbers and a single decimal to be entered into a textbox using regex ------------------------------------------- */ txtnum.addEventListener("input", function (e) { let regex = new RegExp(/^(\d)*(\.)?([0-9]*)?$/, "gm"); if(!regex.test(txtnum.value)) { txtnum.value = txtnum.value.replace(e.data, ""); } });
Try Numbers and Decimal Only
source code home