Javascript validates the price validity instance (2 decimal places must be entered)

  • 2020-03-30 02:48:00
  • OfStack

HTML code:

 Please enter price: 
<input type="text" id="txtProPrice" />
<input type="button" value=" Validate the input " id="btnTest" />

JS code:
<script src="jquery.js">
</script>
<script type="text/javascript" language="javascript">
    function ckProPrice() {
        //Judge the price of goods
        var reg = /(^[-+]?[1-9]d*(.d{1,2})?$)|(^[-+]?[0]{1}(.d{1,2})?$)/;
        if ($("#txtProPrice").val() == "") {
            alert(" Commodity price cannot be empty! ");
            return false;
        } else {
            if (!reg.test($("#txtProPrice").val())) {
                alert(" The market price must be a legal figure ( Positive Numbers, two decimal places at most ) ! ");
                return false;
            } else {
                alert(" Enter legal! yige.org");
                return true;
            }
        }
    }
    $(function() {
        $("#btnTest").click(function() {
            ckProPrice();
        });
    });
</script>


Related articles: