金额验证规则? [英] rule for validating $ amount?
问题描述
一个网站使用jQuery Validation,这是我的新手.我喜欢使用规则并重用它们的方式.我需要验证一个货币字段.它可以是以下格式之一:
$100,000
$2500000
$3503.00
40000000
40033.00
您能帮我如何创建此规则吗?
假设您使用的是 jQuery Validate插件,您可以简单地使用currency规则/方法> additional-methods.js
文件.
$("#form").validate({
rules: {
money: {
currency: ['$', false]
}
}
});
第一个参数是货币符号,第二个参数是一个布尔值,说明货币符号是否应作为用户输入的强制性部分.
演示: 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>
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.
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>
这篇关于金额验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!