在HTML5中,表单可以定义一个属性required来触发默认的校验机制,比如:

<input type="text" required />

弹出的错误提示默认为英文。

 

如果希望自定义语言提示,可以这样:

<input type="text" required oninvalid="InvalidMsg(this);" />

js方法为:

function InvalidMsg(textbox) {
    if (textbox.value == '') {
        textbox.setCustomValidity('不能为空');
    }
    else if (textbox.validity.typeMismatch){
        textbox.setCustomValidity('请输入合法邮箱地址');
    }
    else {
       textbox.setCustomValidity('');
    }
    return true;
}

 

相关文章:

  • 2022-03-04
  • 2022-12-23
  • 2021-10-05
  • 2021-12-17
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-01-12
  • 2022-02-09
  • 2021-08-07
  • 2022-12-23
  • 2021-10-08
  • 2021-11-10
相关资源
相似解决方案