从必需的语句表单更改默认响应后,表现不正常

时间:2016-07-25 18:42:32

标签: html forms validation

所以在我做出这些更改之后,表格不会接受任何输入。这意味着如果我没有输入名字,请说出“请输入名字"”。像它应该的工作。但如果我提出一个名字,它仍然会说同样的话。它不会认出我的名字。在每个字段中都会发生,我必须刷新页面才能使其正常工作。有什么想法吗?

<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="20"    required oninvalid="this.setCustomValidity('Please Enter first name')"></input>    </p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" required oninvalid="this.setCustomValidity('Please Enter last name')"></input>></p>
                                                                                        p>Email Address: <input type="text" name="email" size="20" maxlength="60"    required oninvalid="this.setCustomValidity('Please Enter a valid email')">     </input>></p>
<p>Password: <input type="password" name="pass1" size="10" maxlength="20" required oninvalid="this.setCustomValidity('Please Enter a valid password')">   </input></p>
<p>Confirm Password: <input type="password" name="pass2" size="10"    maxlength="20" required></p>
<p><input type="submit" name="submit" value="Register" /></p>
</form>

1 个答案:

答案 0 :(得分:0)

您需要setCustomValidity(&#39;&#39;)来清除字段的无效状态。

您可以看到它有效here

<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" maxlength="20"
required oninvalid="this.setCustomValidity(this.willValidate?'':'Please Enter first name')"/></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" required oninvalid="this.setCustomValidity(this.willValidate?'':'Please Enter last name')"/></p>
                                                                      <p>Email Address: <input type="text" name="email" size="20" maxlength="60"    required oninvalid="this.setCustomValidity(this.willValidate?'':'Please Enter a valid email')"/></p>
<p>Password: <input type="password" name="pass1" size="10" maxlength="20" required oninvalid="this.setCustomValidity(this.willValidate?'':'Please Enter a valid password')"/> </p>
<p>Confirm Password: <input type="password" name="pass2" size="10"    maxlength="20" required /></p>
<p><input type="submit" name="submit" value="Register" /></p>
</form>