input 的各种属性的验证 checkValidity兼容性

Posted 莫笑我胡为

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了input 的各种属性的验证 checkValidity兼容性相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title></title>
</head>

<body>
    <form onsubmit="testform()" novalidate="true">
        <label for="email"> 邮箱:</label>
        <input type="email" id="email" name="email" />
        <input type="submit" value="提交">
    </form>
</body>


<script>
    function testform() {
        var email = document.getElementById(email);
        if (email.value == ‘‘) {
            alert(请输入email!);
            return false;
        } else if (!email.checkValidity()) {
            alert(email的格式不正确!);
            return false;
        }
    }
</script>

</html>

兼容到IE10及以上

required 等属性也适用

以上是关于input 的各种属性的验证 checkValidity兼容性的主要内容,如果未能解决你的问题,请参考以下文章