I’m attempting to do some validation a price field. I would like to check if the price entered into the price field ends in .99
I’ve attempted find posts about this but I can’t find examples for decimal numbers only whole numbers. I tried to check by doing price % 1
but it isnt consistent as the price increases by 10, 20 etc.
Is there a quick way to check if all numbers end in .99?
const price = 9.99 console.log(price % 1)
Advertisement
Answer
You can try regular expression as well. See following code for example:
function testRegex() { var re = /^[0-9]*[.](99)$/; var val = document.getElementById("inputValue").value; if(re.exec(val)) { document.getElementById("result").innerText = "Found match!!!"; } else { document.getElementById("result").innerText = "Found no match!!!"; } }
<input type="text" id="inputValue" value="" onkeyup="testRegex()" /> <div id="result"></div>