✅ – ✅ – dmikester1 – 15-15 Jul 27

Hoping there are some regex gurus in here. I have an input where I simply need to allow numbers with a possible decimal. I did a lot of googling and stack overflowing.
D
dmikester1266d ago
I found this regex: const re = /^\d+(\.\d{1,2})?$/; so my entire function looks like this:
const changePercent = (val) => {
console.log({ val });
const re = /^\d+(\.\d{1,2})?$/;
if (val === '' || re.test(val)) {
setPercentOfBatch(val);
}
};
const changePercent = (val) => {
console.log({ val });
const re = /^\d+(\.\d{1,2})?$/;
if (val === '' || re.test(val)) {
setPercentOfBatch(val);
}
};
I learned that i can paste in an entire number with a decimal just fine using that. But I cannot type the characters one by one. I assume that is because 12. does not match the regex. That makes sense, thank you.
UU
Unknown User265d ago