问题
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