Skip to content

Instantly share code, notes, and snippets.

@risardi123
Forked from mkmlh/limiterCommasOnInput.js
Created October 14, 2021 15:36
Show Gist options
  • Save risardi123/3ab630630a69e294a63b421060731958 to your computer and use it in GitHub Desktop.
Save risardi123/3ab630630a69e294a63b421060731958 to your computer and use it in GitHub Desktop.
delimiter
const delimiterNumberOnInput = (value, comma) => {
let number = value
if (number.charAt(0) === ",") {
number = ""
} else if (parseFloat(number) === 0 && number.charAt(1) !== ",") {
number = "0"
} else if (number.charAt(0) === "0") {
if (!(number.charAt(1) === ",")) {
number = number.charAt(1)
}
}
if (number.includes(".")) {
number = number.split(".").join("")
}
if (comma) {
number = number.replace(/[^,\d]/g, "")
} else {
number = number.replace(/[^\d]/g, "")
}
number = number.replace(",,", ",")
let splitNumber = number.split(",")
let valueNumber = ""
if (splitNumber[1]) {
splitNumber = [splitNumber[0], splitNumber[1]]
valueNumber = splitNumber[0]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ".")
valueNumber = `${valueNumber}${
splitNumber[1] ? "," + splitNumber[1] : ""
}`
} else {
valueNumber = number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".")
}
console.log(valueNumber)
return valueNumber
}
const delimiterNumberOnText = (value) => {
let number = value
if (number) {
number = number.toString()
if (number) {
let splitNumber = number.split(".")
let valueNumber = splitNumber[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".")
valueNumber = `${valueNumber}${
splitNumber[1] ? "," + splitNumber[1] : ""
}`
return valueNumber
}
return value
} else {
return "0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment