I encountered a problem today when working on validating a form. The user has to input a price in the price field. If they input 0 (for some reason) or leave it empty, then my following code catches this:
var pricevar = $("#price").val();
if (pricevar == '' || pricevar == '0') {
var errormessage = 'Product Price is empty or zero. Please enter it above.';
$("#errordiv").html(errormessage).slideDown();
return false;
} else {
return false;
}
My input field is as follows:
<input type="text" name="price" id="price" placeholder="0.00">
However, if (again, for some reason) the user enters 0.00 then this isn't detected and the form gets submitted. I have tried null
and empty
but nothing is working. Does anyone know how I detect that 0.00 is an empty number, but that 0.01 (or great) is not?