jQuery验证 - 允许一些没有前导零? [英] Jquery validation - allow number without the leading zero?
问题描述
我使用jQuery验证(http://docs.jquery.com/Plugins/Validation)像这样:
I'm using jquery validation (http://docs.jquery.com/Plugins/Validation) like so:
var $__field = $("#selector");
if ($__field.is(":visible")) {
$__field.rules('add', { required: true });
$__field.rules('add', { number: true });
}
如果用户输入一个数字,没有前导零,例如0.5则jQuery验证说请输入一个有效的数字。
If the user enters a number without a leading zero, eg .5 then jquery validation says "Please enter a valid number."
我怎样才能更改验证允许输入号码,而无需前导零?
How can I change the validation to allow number entry without requiring the leading zero?
推荐答案
现在的问题是与用于数字常规前pression。在最新版本,系1021年至1023年,我们有:
The problem is with the regular expression used for numbers. Looking at the latest version, lines 1021-1023, we've got:
number: function(value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
},
的问题是,这第一组块,它看起来为主导负号和数字,无论是个人还是在三个由逗号分隔的基团,是不可选的:
The problem is that this first chunk, which looks for leading negative sign and numbers, either individual or in groups of three separated by commas, is not optional:
(?:\d+|\d{1,3}(?:,\d{3})+)
后跟踪一个
在年底将使其可选:
(?:\d+|\d{1,3}(?:,\d{3})+)?
通过此更新,您可以修改您的本地插件的副本,改变了常规的前pression修改后的版本,或添加的更换验证方法您的javascript:
With this update, you can either modify your local copy of the plugin, changing the regular expression to the modified version, or add a replacement validation method to your javascript:
jQuery.validator.addMethod("number", function(value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
}, "Please enter a valid number.");
在任何与此页 addMethod
电话,数量
验证方法将被替代做了多次验证了不需要前导零。
On any page with this addMethod
call, the number
validation method will be overridden to do a number validation that doesn't require a leading zero.
这篇关于jQuery验证 - 允许一些没有前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!