Jquery Validation 插件在 Chrome 中工作,但在 Mozilla 中不起作用



我在Django应用程序中有一个表单,其验证在Chrome上完美运行,但我无法让它在Mozilla上运行。 我加载了这个版本的jquery和jquery验证插件

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.19.1/jquery.validate.js"></script>
<script src="https://ajax.microsoft.com/ajax/jquery.validate/1.19.1/additional-methods.js"></script>

我的jquery验证代码如下所示:

jQuery.validator.addMethod("phoneValidator", function(value, element) {
return this.optional(element) || /^+?d?(?([d]{3}?))?-?([d]{3}?)-?([d]{4,5})$/.test(value);
}, 'Please enter a valid phone number. E.g. 123-456-7890');
jQuery.validator.addMethod("zipValidator", function(value, element) {
return this.optional(element) || /(?i)^[a-z0-9][a-z0-9- ]{0,10}[a-z0-9]$/.test(value);
}, 'Please enter a valid zip code. E.g. 80111 or 1234-80111');
jQuery.validator.addMethod("dateValidator", function(value, element) {
return this.optional(element) || /^(0[1-9]|1[0-2])[/|-](0[1-9]|1d|2d|3[01])[/|-]([1-9]d{3})$/.test(value);
}, jQuery.validator.format('Please enter a valid date in mm/dd/yyyy format.'));
jQuery.validator.addMethod("dateNotInFutureValidator", function(value, element) {
//replace all dashes with /
value = value.replace(/-/g, '/');
value = value.split('/');
var today = new Date();
var month = today.getMonth() + 1; //getMonth is 0-indexed
var day = today.getDate();
var year = today.getFullYear();
// console.log(month, day, year);
// console.log(value[0], value[1], value[2]);
return this.optional(element) || (value[2] == year && value[0] == month && value[1] <= day) || (value[2] == year && value[0] < month) || (value[2] < year);
}, jQuery.validator.format('Please enter a date that is not in the future.'));
jQuery.validator.addMethod("allChars", function(value, element) {
return this.optional(element) || /[a-zA-Z]+$/.test(value);
}, 'This field may only contain characters.');
jQuery.validator.addMethod("allHebrew", function(value, element) {
return this.optional(element) || /^[u0590-u05FF]+$/.test(value);
}, 'This field may only contain Hebrew characters.');
FORM_RULES = {
verify_email: { equalTo: '#id_email' },
'{{soldier.date_of_birth.name }}' : {'dateValidator': true, 'dateNotInFutureValidator':true},
'{{soldier.legal_first_name.name}}' : 'allChars',
'{{soldier.legal_last_name.name}}' : 'allChars',
'{{soldier.naaleh_selah_choice.name}}' : {
required: function(element){
return $('#id_naaleh_selah_bool_0').val() == 1
}},
accept_checkbox: {required: true}
};
FORM_MESSAGES = {
verify_email: {equalTo: 'Please enter the same value as the Email field.'},
accept_checkbox: {required: 'You must accept the terms and conditions to continue.'}
};

$('#intake_form').validate({
rules: FORM_RULES,
messages: FORM_MESSAGES,
errorPlacement: function(error, element)
{
if ( element.is(":radio"))
{
error.insertAfter( element.parent().parent().parent() );
}
else if (element.attr("id") === "accept_checkbox")
{
error.insertAfter(element.parent());
}
else { // This is the default behavior of the script
error.insertAfter( element );
}
}
});

Mozilla Firefox 上没有显示任何验证消息,它让我无需填写必填字段即可提交表单。 我确实在Mozilla的控制台中看到以下错误:

SyntaxError: invalid regexp group

但我不确定如何处理它,因为它说错误是 1:1:1 - html 页面源代码中的空白行。
我确实在某处读到,Firefox 不支持后视正则表达式语法,并且 jquery 验证插件确实允许使用正则表达式,所以也许两者不兼容? 有没有办法解决这个问题?

问题是我的zipValidator(发布的验证代码中的第二个方法调用(有一个被Firefox认为无效的正则表达式。其他浏览器支持(?i)表示法(不区分大小写(,但 Firefox 不支持。 一旦我更改了该正则表达式(只是在每个 a-z 之后添加了 A-Z(,所有验证以及使用 jquery 显示和隐藏都有效。

相关内容

最新更新