我已经做了几次不同的事情。第一个是在控制器中进行验证,但我清理了它并结束了在客户端的 jquery 中进行电子邮件验证。
这样,您就不必为不匹配的电子邮件来回走动。
这是一个用于检查电子邮件是否有效的示例 javascript 函数。现在您所要做的就是再次验证输入的第一个。
您只需要表单中的另一个字段参数用于第二封电子邮件。
// simple function to check if a email is of a valid email type
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
// method to check if email and confermation email is the same.
$("#signupForm").validate({
rules: {
...snip...
email1: {
required: true,
minlength: 5
},
email2: {
required: true,
minlength: 5,
equalTo: "#email1"
},
...snip...
},
messages: {
...snip...
email: {
required: "Please provide a email",
minlength: "Your email must be at least 5 characters long"
},
email2: {
required: "Please provide a confermation email",
minlength: "Your confermation email must be at least 5 characters long",
equalTo: "Please enter the same email as above"
},
...snip...
}
});