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?
JavaScript
x
3
1
const price = 9.99
2
3
console.log(price % 1)
Advertisement
Answer
You can try regular expression as well. See following code for example:
JavaScript
1
11
11
1
function testRegex() {
2
var re = /^[0-9]*[.](99)$/;
3
var val = document.getElementById("inputValue").value;
4
5
if(re.exec(val)) {
6
document.getElementById("result").innerText = "Found match!!!";
7
} else {
8
document.getElementById("result").innerText = "Found no match!!!";
9
}
10
11
}
JavaScript
1
2
1
<input type="text" id="inputValue" value="" onkeyup="testRegex()" />
2
<div id="result"></div>