gpt4 book ai didi

javascript - 谁能告诉我为什么这个电子邮件验证不起作用?

转载 作者:行者123 更新时间:2023-11-28 16:56:31 24 4
gpt4 key购买 nike

我正在尝试使电子邮件验证正常工作,但似乎无法找出问题所在。如果输入的电子邮件不是有效的电子邮件,我希望它发出警报。我附加的代码是电子邮件功能和电子邮件的表单代码。感谢您的帮助!

  {
var emailfield=document.getElementById("emailaddress").value;
var atpos=emailfield.indexOf("@");
var dotpos=emailfield.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailfield.length){
alert("Please enter a valid email address with an @ and proper domain.")
return false;

}
}

这是表单的代码。

     <form action='#' method='post' name='f1' id="VPN" onsubmit='return checkButons(this)'> 


Aspects <br>
<input type='radio' name='aspect' id='aspect1' value='security' />Security <br>
<input type='radio' name='aspect' id='aspect2'value='speed' /> Speed<br>


Features:<br>

<input type='checkbox' name='feat1' id='feat1'value='highspeedvpn' /> High Speed VPN <br>
<input type='checkbox' name='feat2' id='feat2' value='transactionguard' />Transaction Guard <br>
<input type='checkbox' name='feat3' id='feat3'value='antivirus' /> Antivirus Addon <br>






<h3 id="generaltext">Fill out your information below:</h3>

<p>
Name:<input name="Name"id='name'size=:50 type="text" required><br>
</p>
<p>

Email Address:<input name="Emailaddress"id='emailaddress'size=:50 type="text" required><br>
</p>
<p>
Street Address:<input name="S_Address"id='s_address'size=:50 type="text" required><br></p>
<p>
Address 2:<input name="Address_2"id='address_2'size=:50 type="text" required><br></p>
<p>
Zip Code:<input name="Zip"id='zip'size=:50 type="text" required><br>
<p>
City:<input name="City"id='city'size=:50 type="text" required><br>

最佳答案

您可以使用常规 exp 进行验证 -

var email = text_email.value;
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (re.test(email)) {
alert("Error msg");
return false;
}

关于javascript - 谁能告诉我为什么这个电子邮件验证不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56470173/

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