dmikester1
dmikester12y ago

✅ – ✅ – 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.
3 Replies
dmikester1
dmikester1OP2y 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.
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1134141929949515817
reactibot
reactibot2y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1134141929949515817 This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1134141929949515817