rule for validating $ amount?

不问归期 提交于 2019-12-23 02:42:16

问题


A web site uses jQuery Validation which I'm new to. I like the way it works using rules and reusing them. I need to validate a field for money. It can be one of the following formats:

$100,000
$2500000
$3503.00
40000000
40033.00

Can you please help me how to create this rule.


回答1:


Assuming you're using the jQuery Validate plugin, you can simply use the currency rule/method as provided in the additional-methods.js file.

$("#form").validate({
    rules: {
        money: {
            currency: ['$', false]
        }
    }
});

The first parameter is the currency symbol and the second parameter is a boolean stating whether the currency symbol should be a mandatory part of the user's input.

DEMO: http://jsfiddle.net/dxrd4cp7/

$(document).ready(function() {

  $("#form").validate({
    rules: {
      money: {
        currency: ['$', false]
      }
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.js"></script>


<form id="form">
  <input type="text" name="money" />
  <br/>
  <input type="submit" />
</form>


来源:https://stackoverflow.com/questions/26855843/rule-for-validating-amount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!