需要HTML5占位符 - 更改默认消息

时间:2013-07-03 07:22:59

标签: html5 message default placeholder required

我很感兴趣是否可以更改占位符的默认验证消息。
必须是一种制作自定义信息的方式。

我的表单:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Placeholder</title>
</head>

<body>
    <form>
        <input type="text" id="email" maxlength="35" placeholder="E-mail" required>
        <button type="submit">Ok</button>
    </form>
</body>
</html>

我尝试使用JavaScript ,但每次都会显示以下消息:

<script type="text/javascript">
document.getElementById('email').setCustomValidity('Please enter your e-mail!');
</script>

1 个答案:

答案 0 :(得分:0)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>
</head>

<body>
    <form>
        <input type="text" id="email" maxlength="35" placeholder="E-mail" required>
        <button type="submit">Ok</button>
    </form>
    <script>
    $(document).ready(function(){
    var elements = document.getElementsByTagName("INPUT");
    for (var i = 0; i < elements.length; i++){
        elements[i].oninvalid = function(e){
            e.target.setCustomValidity("");
            if (!e.target.validity.valid){
                e.target.setCustomValidity("This field cannot be left blank");
            }
        };
        elements[i].oninput = function(e){
            e.target.setCustomValidity("");
        };
    }
    })
    </script>
</body>

</html>