最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Validate only decimal number - Stack Overflow

programmeradmin5浏览0评论

Does anyone know how I can validate a price format like the following

1.00 1200.00, but I also want to allow as 1 and 1200

$.validator.addMethod('decimal', function(value, element) {
        return this.optional(element) || /^[0-9]+\.\d{1,3}$/.test(value);
    }, "Please enter a correct number, format 0.00");

jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });

    $( "#myform" ).validate({
        rules: {
            field: {
                required: true,
                decimal: true
            }
        }
    });

^ I found this code, but it still allows mas. Basically I am looking for something like is numeric without the ma.

So basically only want to allow numbers and optional decimals.

Any suggestion?

Update: So the above works, but its forcing the requirement of decimal. How to make that optional so I can do 1200 or 1200.00? Or maybe there is a way to just convert 1200 to 1200.00 ?

Does anyone know how I can validate a price format like the following

1.00 1200.00, but I also want to allow as 1 and 1200

$.validator.addMethod('decimal', function(value, element) {
        return this.optional(element) || /^[0-9]+\.\d{1,3}$/.test(value);
    }, "Please enter a correct number, format 0.00");

jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    });

    $( "#myform" ).validate({
        rules: {
            field: {
                required: true,
                decimal: true
            }
        }
    });

^ I found this code, but it still allows mas. Basically I am looking for something like is numeric without the ma.

So basically only want to allow numbers and optional decimals.

Any suggestion?

Update: So the above works, but its forcing the requirement of decimal. How to make that optional so I can do 1200 or 1200.00? Or maybe there is a way to just convert 1200 to 1200.00 ?

Share Improve this question edited Mar 15, 2016 at 23:45 limit asked Mar 15, 2016 at 23:35 limitlimit 6472 gold badges8 silver badges28 bronze badges 4
  • Delete the ma from that [ ] part of the regular expression. – Pointy Commented Mar 15, 2016 at 23:35
  • Also see this question, which for me at least is at the top of the "Related" section over there ========> – Pointy Commented Mar 15, 2016 at 23:37
  • Removing the ma worked. Yeah i saw the isNumeric(), but not exactly sure how to tie into $.validator. I want to also make sure that just digits can be entered so 1200 or 1200.00 – limit Commented Mar 15, 2016 at 23:44
  • You can make the decimal part optional by (?:\.\d{1,3})?. Note that you won't match any negative number. – Sebastian Proske Commented Mar 15, 2016 at 23:48
Add a ment  | 

1 Answer 1

Reset to default 4

My proposal to solve your problem is:

$.validator.addMethod('decimal', function(value, element) {
  return this.optional(element) || /^((\d+(\\.\d{0,2})?)|((\d*(\.\d{1,2}))))$/.test(value);
}, "Please enter a correct number, format 0.00");


$(function () {
  $("#frm").on('submit', function(e) {
    e.preventDefault();
    $('#log').text('Form is valid? ' + $(this).valid().toString());
  });

  $("#frm").validate({
    rules: {
      price: {
        required: true,
        decimal: true
      }
    }
  });
});
<script src="http://code.jquery./jquery-1.11.3.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>

<form id="frm">
    Money: <input name="price" id="price" type="text"><br>
    <input id="submit" type="submit" value="Submit">
</form>
<p id="log"></p>

发布评论

评论列表(0)

  1. 暂无评论