gpt4 book ai didi

javascript - 提交电子邮件验证时未添加 innerHTML

转载 作者:行者123 更新时间:2023-11-30 19:04:03 24 4
gpt4 key购买 nike

如果有人在我的表单中输入了错误的电子邮件,我将尝试操纵 DOM。发生的事情是页面上似乎没有发生任何事情。下面是我的 html 代码。

function ValidateEmail(inputText) {
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (inputText.value.match(mailformat)) {
alert("You have entered a correct email addresss")
document.form1.text1.focus();
return true;
} else {
document.getElementById("addedText").innerHTML += " this has just been added";
// alert("You have entered an invalid email address!");
document.form1.text1.focus();
return false;
}
}
<div class="email-entry desktop-container">
<div id="a"></div>
<form name="form1" action="#">
<input id="test" type="text" name="text1" value="" placeholder="Email Address">
<input type="image" value="validate" onclick="ValidateEmail(document.form1.text1)" src="images/icon-arrow.svg" alt="submit">
<p id="addedText"></p>
</form>

</div>

当我添加一个警报时,它似乎起作用了。不确定我在定位元素时做错了什么。

最佳答案

当您单击图像时,它就像一个提交按钮并提交表单。您不取消点击。您缺少点击返回,因此 return false 被忽略。

function ValidateEmail(inputText) {
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (inputText.value.match(mailformat)) {
alert("You have entered a correct email addresss")
document.form1.text1.focus();
return true;
} else {
document.getElementById("addedText").innerHTML += " this has just been added";
// alert("You have entered an invalid email address!");
document.form1.text1.focus();
return false;
}
}
<div class="email-entry desktop-container">
<div id="a"></div>
<form name="form1" action="#">
<input id="test" type="text" name="text1" value="" placeholder="Email Address">
<input type="image" value="validate" onclick="return ValidateEmail(document.form1.text1)" src="images/icon-arrow.svg" alt="submit">
<p id="addedText"></p>
</form>

关于javascript - 提交电子邮件验证时未添加 innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com