即使输入有效,也会弹出自定义验证消息

Mul*_*ner 6 html validation html5

我正在尝试为输入定义自己的无效错误消息type='email'.

我使用下面的HTML.问题是,在提交输入时,如果输入值有效,我会收到自定义错误消息EVEN.

<input name="resp_mail" required="required" onchange="try{setCustomValidity(' ')}catch(e){}" onkeypress="try{setCustomValidity(' ')}catch(e){}" oninvalid="setCustomValidity('Custom error message')" type="email" placeholder="Enter mail" />
Run Code Online (Sandbox Code Playgroud)

也看到这个小提琴

sti*_*ksu 6

如果您更改顺序以使oninvalid首先并删除onchange和onkeypress的空间,在我看来它是有效的.

<input 
  name="resp_mail"
  required="required" 
  oninvalid="setCustomValidity('Custom error message')" 
  onchange="try{setCustomValidity('')}catch(e){}" 
  onkeypress="try{setCustomValidity('')}catch(e){}" 
  type="email" 
  placeholder="Enter mail"
/>
Run Code Online (Sandbox Code Playgroud)