Can you help me validate user input to ensure it is a positive currency value in the correct format?
Examples of valid formats include: 0.5
, 0.55
, 5
(please note this one), 5.5
, 5.55
, 55
, etc.
This is the code I am currently using:
if ($("#gross").val() > 0 && !/^\d+?\.?\d?\d$/.test($("#gross").val())) {
alert($("#gross").val() + " is invalid currency");
}
The current issue is that the code works for most cases, but it does not work for single digits like 5
(and 5.
). However, it does work for values like 5.5
.
Any insights on what I might be doing wrong?