当用户尚未键入他们的电子邮件时,我能够更改错误消息,但是当用户以错误的格式键入电子邮件时,如何更改错误消息呢?
这是我用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>发布于 2019-12-02 13:11:44
只需将标题添加到输入标记即可修复此问题。
<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 "@". Example: info@roshni.nl')" title="Email: the email contains '@'. Example: info@ros-bv.nl" />
那么,你的应该是:
<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>
,不客气,
发布于 2018-06-02 02:17:30
下面是一个如何做到这一点的例子:
<!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('')">https://stackoverflow.com/questions/50652783
复制相似问题