我在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]|1\d|2\d|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进行显示和隐藏就可以了。