jquery 验证规则和 autoNumeric 插件
我有一个验证规则,如下所示:
loanAmount:{required:true,number:true,range:[20000,500000]},
我还想对 LoanAmount 字段进行货币格式设置。因此,我使用了 autoNumeric 插件,如下所示:
$('#loanAmount').autoNumeric({dGroup:2});
但是,使用此插件后,loanAmount 的范围验证无法正常工作。 jquery 获取的是格式化值,而不是获取 LoanAmount 的原始值,因此无法进行验证。 例如,当我输入 25000 时,它会变成 25,000 并传递给 jquery,并且验证不会按预期进行。
有办法解决这个问题吗?
如果有人可以提供帮助,我会很高兴。
谢谢, 索拉布
I have a validation rule as shown below:
loanAmount:{required:true,number:true,range:[20000,500000]},
I also want to do currency formatting for the loanAmount field. So I have used the autoNumeric plugin as shown below:
$('#loanAmount').autoNumeric({dGroup:2});
However, with this the range validation for the loanAmount does not work properly. Instead of getting the raw value for loanAmount, jquery gets the formatted value and hence is not able to validate.
For instance, when I enter 25000, it becomes 25,000 and is passed to jquery and the validation does not work as expected.
Is there a way around this?
Will be glad if someone can help.
Thanks,
Saurabh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
老问题,但如果有人发现这个问题,可以通过添加自定义验证来解决方法。您可以:
下面是如何使用 AutoNumeric 为货币格式输入添加最小值验证的示例
Old question, but if anybody finds this issue, there is a workaround by adding a custom validation. You can:
Here is an example of how to add a validation of a minimun value for a money formatted Input with AutoNumeric
当我一起使用 autoNumeric 和 jQueryValidation 时,我遇到了同样的问题。
输入值可以使用 jQueryValidation 中的 normalizer 进行编辑。
您可以使用 addClassRules 函数根据类名进行这种排列。在下面的示例中,我只是删除了逗号。
I had the same issue as I used autoNumeric and jQueryValidation together.
Input value can be edited with normalizer in jQueryValidation.
You can make this arrangement according to the class names by using the addClassRules function. In the examples below I just removed the commas.
不幸的是,这是一个常见问题,没有现成的解决方案(据我所知)。
您可以定义自己的验证方法,该方法将在执行检查之前首先“取消格式化”该值。 可以在此处找到示例。
提交表单时,传递的值是否已格式化?在这种情况下,您可以定义验证规则来检查您的特定格式而不是纯范围,例如通过正则表达式。
我认为这是一个值得向插件开发人员建议的功能:一个可选的处理程序,将在验证值之前调用,例如可以取消该值的格式。
希望这会有所帮助,d。
Unfortunately, this a regular problem that has no out of the box solution (well not in my knowledge).
What you can do is define your own validation method that will first 'unformat' the value before performing the check. You can find an example here.
When submitting the form, is the value passed formatted ? In this case you can defined your validation rule to check your specific formatting instead of a pure range, via a regex for example.
I think this is a feature that would be worth proposing to the plugin developer: an optional handler that will be called prior to validating the values in which one would be able to for instance unformat the value.
Hope this helps, d.
@Saurabh:我找到了解决方案。还有另一个插件可以完美地解决您面临的问题。您只需通过以下链接下载即可:
https://www.customd.com/articles/14/jquery-number -format-redux
文档也在那里...
希望有帮助。
@Saurabh: I found a solution for that. There is another plugin which is working perfectly fine with the issue you are facing. You can simply download it on the link below:
https://www.customd.com/articles/14/jquery-number-format-redux
The documentation is there as well...
Hope It helps.
我今天遇到了同样的问题,这就是我的做法,首先我定义了新的自定义规则(针对货币):
然后:
我有一个简单的函数
priceToNumber
来清理围绕货币值的格式。如果需要,您可以使用 AutoNumeric 中的getNumericString()
。类似于:
在验证器方法中。
I got same problem today, here is how I did, first I defined the new custom rule (for money) :
Then :
I have a simple function
priceToNumber
to clean up format around a money value. You can usegetNumericString()
from AutoNumeric if needed.Something like :
inside the validator method.