Bootstrap 如何更改默认的“请在电子邮件地址中包含@”?

l3zydbqr  于 12个月前  发布在  Bootstrap
关注(0)|答案(2)|浏览(263)

当用户还没有输入电子邮件时,我可以更改错误消息,但是当用户输入错误格式的电子邮件时,我如何更改错误消息?
这是我用Bootstrap 4风格写的代码:

<div class="form-group">
    <label>Email</label>
    <input type="email" name="email" required="" class="form-control" oninvalid="this.setCustomValidity('Please Enter valid email')" oninput="setCustomValidity('')">
</div>

字符串

67up9zun

67up9zun1#

您可以简单地通过向输入标记添加标题来解决此问题

<input type="email" class="form-control" name="email" id="freeform_email"
                        placeholder="Enter an email" required="required"
                        oninvalid="this.setCustomValidity('Enter an email that contains &quot@&quot. Example: [email protected]')" title="Email: the email contains '@'. Example: [email protected]" />

字符串
你的应该是:

<div class="form-group">
    <label>Email</label>
    <input type="email" name="email" required="" class="form-control" oninvalid="this.setCustomValidity('Please Enter valid email')"  oninput="this.setCustomValidity('')" title='<your text>'">
</div>

不客气

ttcibm8c

ttcibm8c2#

下面是一个如何做到这一点的例子:

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
    var x = document.forms["myForm"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        return false;
    }
}
</script>
</head>

<body>
<form name="myForm" action="/action_page.php" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>

字符串
您必须更改alert中的文本以包含您的自定义消息,并更改action_page.php中的文本以包含您自己的消息并添加所需的样式!

更新

如果你不想实现自己的JavaScript函数,那么你可以继续向oninvalid添加条件,如下所示:

<input type="email" name="email" required="" class="form-control" oninvalid="if (this.value == ''){this.setCustomValidity('This field is required!')} if (this.value != ''){this.setCustomValidity('The email you entered is invalid!')}" oninput="setCustomValidity('')">

相关问题