price format
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
document.querySelectorAll(".price-input").forEach((input) => {
|
||||
input.value = format(input.value);
|
||||
|
||||
input.addEventListener("input", () => {
|
||||
const cursor = input.selectionStart;
|
||||
const raw = input.value.replace(/,/g, "");
|
||||
input.value = format(raw);
|
||||
input.setSelectionRange(cursor, cursor);
|
||||
});
|
||||
input.form.addEventListener("submit", () => {
|
||||
input.value = input.value.replace(/,/g, "");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const format = (value) => {
|
||||
if (!value) return "";
|
||||
return value.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||||
};
|
||||
Reference in New Issue
Block a user