[英]Validating e-mail address
I am trying to validate an e-mail address using javascript. 我正在尝试使用javascript验证电子邮件地址。 The problem is the if statements aren't executing correctly.
问题是if语句执行不正确。 If I delete the 'else statement' the code runs correnctly and the page does not load with errors.
如果删除“ else语句”,则代码会正确运行,并且该页面不会加载错误。 If I include the 'else statement' the else statement never executes and the status bar says that the page loads with errors.
如果我包含“ else语句”,则else语句将永远不会执行,并且状态栏显示页面加载有错误。 I was wondering if anyone can find any errors that I am unable to pick up?
我想知道是否有人可以找到我无法发现的错误?
<h4>Example 4:</h4>
<div style="border:3px dashed #aaa; width:200px;">
<div id="text">e-mail: <input id="email" onblur="verifyEmail()"/></div>
<div id="verification" > </div>
</div>
<script type="text/javascript">
function verifyEmail(){
var email = document.getElementById("email").value;
var atPos=0;
var dotPos=0;
atPos = email.indexOf("@");
dotPos = email.lastIndexOf(".");
if(atPos<=3 || dotPos<atPos+2 || dotPos+2>=email.length){
document.getElementById("verification").innerHTML = "Invalid E-mail Address";
}else{
document.getElementById("verification").innerHTML = "Valid";
}
}
</script>
<script language="javascript">
function checkEmail() {
var email = document.getElementById('emailaddress');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
</script>
Read more: http://www.marketingtechblog.com/javascript-regex-emailaddress/#ixzz1wqetPJQQ 了解更多: http : //www.marketingtechblog.com/javascript-regex-emailaddress/#ixzz1wqetPJQQ
try this 尝试这个
function validateEmail(email) {
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,}))$/;
return re.test(email);
}
Demo : Here is the demo 演示 : 这是演示
OR a more simpler way is 或更简单的方法是
function validateEmail(email)
{
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
if you are using HTML5 try <input type="email"...
please note. 如果您使用的是HTML5,请尝试
<input type="email"...
请注意。 this one works if the input is in a form tag with a submit button and isn't handled by JS 如果输入是带有提交按钮的表单标签中的,并且不是由JS处理的,则此方法有效
<input type="email" placeholder="me@example.com">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.