gpt4 book ai didi

jquery 表单验证允许提交表单,即使有错误

转载 作者:行者123 更新时间:2023-12-01 04:37:06 25 4
gpt4 key购买 nike

使用jquery验证插件,如果我第一次提交表单出现错误,那么它将不允许表单提交并显示错误。但是,如果我只修复一个表单元素错误而其他元素仍然无效,则表单将提交到表单操作 URL。

除非用户修复了所有错误,否则我希望表单不要提交。我试图在提交处理程序中返回 false 但徒劳。

jQuery(function ($) {
$('#regform').validate({
rules: {
fullname: {
required: true,
minlength: 2,
maxlength: 20,
lettersonly: true
},
gender: {
required: true
},
mobile: {
required: true,
minlength: 10,
maxlength: 13,
digits: true
},
address: {
required: true,
minlength: 10,
},
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 6

}
},
messages: {
fullname: {
required: "Please enter your full name",
minlength: "Name should be more than 2 characters",
maxlength: "Name should be less than 20 characters",
lettersonly: "Name should contain only letters"
},
gender: {
required: "Please select your gender",
},
mobile: {
required: "Please enter your mobile number",
minlength: "Mobile number should be more than 10 characters",
maxlength: "Mobile number should be less than 13 characters",
digits: "Mobile number should contain only digits"
},
address: {
required: "Please enter your address",
minlength: "Address should be more than 10 characters",
},
email: {
required: "Please enter your email address",
email: "Please enter a valid email address"
},
password: {
required: "Please provide a password",
minlength: "password must be at least 6 characters"
}
},
submitHandler: function(form) {
var valid = $("regform").valid();
if(!valid) {
e.preventDefault();
alert("stop");
} else {
e.preventDefault();
}

}
});
});

最佳答案

使用类似的设置,但字段较少 - https://codepen.io/jefftimbre/pen/xWjQrX?editors=1010

lettersonly 方法可能已被删除或弃用。将以下内容添加到您的代码中,它应该可以工作。让我知道。

$.validator.addMethod( "lettersonly", function( value, element ) {
return this.optional( element ) || /^[a-z]+$/i.test( value );
}, "Letters only please" );

关于jquery 表单验证允许提交表单,即使有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49602917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com