Javascript:为什么 setCustomValidaty 在第一次提交时没有出现,但在第二次提交时却出现了?



我试图验证登录表单,但我无法理解为什么当我输入错误的消息时,setCustomValidation不显示我第一次点击提交按钮(实际输入)的原因。然而,当我点击同一按钮第二次出现错误信息,因为它应该。为什么呢?代码如下:

function validate(){
console.log("check validate()")

var email = document.getElementById("email");
var psw = document.getElementById("psw");
const patt =  /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
if (email.value=="" && psw.value==""){
email.setCustomValidity("You need to insert email and password!");
return false;
}
else if ( email.value==""){
email.setCustomValidity("Insert your email address");
return false;
}

else if (psw.value==""){
psw.setCustomValidity("Insert password");
return false;
}
else if ( !patt.test(email.value) ){
email.setCustomValidity("This is not an email!");
console.log("Subcase works");
return false;
}
}
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Game</h1>
<div>
<form onsubmit="return validate()" method="POST" action="login.php">
<input id="email" type="text" placeholder="email">
<input id="psw" type="password" placeholder="password">
<input type="submit" id="login-btn" value="Accedi">
</form>
</div>
</body>
</html>

首先,根据setCustomValidity的文档:

您必须在同一元素上调用reportValidity方法,否则什么都不会发生。

它第二次工作的原因是因为当自定义有效性消息设置时,当"提交";按钮再次单击时,浏览器内置的表单验证将接管并阻止提交。这就是为什么你看不到"check validate()";后续提交时控制台日志中的消息。

因此,仅仅在email.setCustomValidity之后添加email.reportValidity()之类的东西是而不是一个解决方案,因为在随后的提交中,提交事件处理程序将不会被调用,因为表单由于非空的自定义有效性消息而永远不会被提交。如果您尝试这样做,您将看到即使在填写了电子邮件和密码字段之后也会得到相同的错误消息。要解决这个问题,您可以将novalidate添加到表单以绕过浏览器验证,或者您可以在使用输入的onchange事件更改输入时清除自定义有效性消息。

下面是一个工作示例,将novalidate添加到表单并使用reportValidity()

function validate(){
console.log("check validate()")

var email = document.getElementById("email");
var psw = document.getElementById("psw");
const patt =  /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
if (email.value=="" && psw.value==""){
email.setCustomValidity("You need to insert email and password!");
email.reportValidity();
return false;
}
else if ( email.value==""){
email.setCustomValidity("Insert your email address");
email.reportValidity();
return false;
}

else if (psw.value==""){
psw.setCustomValidity("Insert password");
psw.reportValidity();
return false;
}
else if ( !patt.test(email.value) ){
email.setCustomValidity("This is not an email!");
email.reportValidity();
console.log("Subcase works");
return false;
}
}
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Game</h1>
<div>
<form onsubmit="return validate()" method="POST" action="login.php" novalidate>
<input id="email" type="text" placeholder="email">
<input id="psw" type="password" placeholder="password">
<input type="submit" id="login-btn" value="Accedi">
</form>
</div>
</body>
</html>

下面是一个在输入字段上使用onchange事件并使用reportValidity()的工作示例。注意,在本例中,onsubmit处理程序只在有效性消息被清除后调用,而不是每次单击提交按钮时调用。

function validate(){
console.log("check validate()")

var email = document.getElementById("email");
var psw = document.getElementById("psw");
const patt =  /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
if (email.value=="" && psw.value==""){
email.setCustomValidity("You need to insert email and password!");
email.reportValidity();
return false;
}
else if ( email.value==""){
email.setCustomValidity("Insert your email address");
email.reportValidity();
return false;
}

else if (psw.value==""){
psw.setCustomValidity("Insert password");
psw.reportValidity();
return false;
}
else if ( !patt.test(email.value) ){
email.setCustomValidity("This is not an email!");
email.reportValidity();
console.log("Subcase works");
return false;
}
}
<!DOCTYPE html>
<html lang="eng">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Game</h1>
<div>
<form onsubmit="return validate()" method="POST" action="login.php">
<input id="email" type="text" placeholder="email" onchange="event.target.setCustomValidity('')">
<input id="psw" type="password" placeholder="password" onchange="event.target.setCustomValidity('')">
<input type="submit" id="login-btn" value="Accedi">
</form>
</div>
</body>
</html>

相关内容

最新更新