jQuery validate插件-货币 [英] jQuery validate plugin - currency
问题描述
我正在尝试找出如何验证jQuery validate插件中的数字为合法的美国货币,最多两个小数位零,而没有逗号或美元符号(25000.00、1.00等)
I am attempting to figure out how to validate a number in the jQuery validate plugin as legitimate US currency, max two decimal place zeroes, without commas or dollar signs (25000.00, 1.00 , etc)
我尝试了这个要点 https://gist.github.com/jonkemp/9094324 ,但似乎需要美元符号($)
I attempted this gist https://gist.github.com/jonkemp/9094324, but it seems to require the Dollar sign ($)
jQuery.validator.addMethod("currency", function (value, element) {
return this.optional(element)
|| /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(value);
}, "Please specify a valid amount");
我还尝试了此堆栈溢出使用jQuery验证货币的客户端验证字段 但验证码不会验证带有两个零(例如1.00)的数字
I also attempted this Stack Overflow cliente validation using jQuery validate for currency fields but the validation code will not validate a number with two zeroes (1.00 for instance)
jQuery.validator.addMethod("currency", function(value, element) {
var isValidMoney = /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
return this.optional(element) || isValidMoney;
},
"Must be monetary (0 or 0.00)"
);
这是显示这些问题的jsfiddle http://jsfiddle.net/d4ywy0fu/
here is the jsfiddle that is showing these issues http://jsfiddle.net/d4ywy0fu/
感谢您的帮助
推荐答案
我正在尝试找出如何验证jQuery validate插件中的数字为合法的美国货币,最多两个小数位零,而没有逗号或美元符号(25000.00、1.00等)
I am attempting to figure out how to validate a number in the jQuery validate plugin as legitimate US currency, max two decimal place zeroes, without commas or dollar signs (25000.00, 1.00 , etc)
只需包含 additional-methods.js
文件,然后按如下所示使用内置的currency
方法...
Simply include the additional-methods.js
file and use the built-in currency
method as follows...
currency: ["$", false] // dollar sign optional
currency: "$" // dollar sign required (default)
注意:在所有情况下,逗号都是可选的.
Note: commas are optional in all cases.
演示: http://jsfiddle.net/d4ywy0fu/2/
DEMO: http://jsfiddle.net/d4ywy0fu/2/
注意:您的jsFiddle 还包含
NOTE: Your jsFiddle also contains a DOM ready handler inside of another DOM ready handler. While it's not harmful, it's totally superfluous.
$(function() { // <- DOM Ready Handler
$(document).ready(function() { // <- DOM Ready Handler
....
这篇关于jQuery validate插件-货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!