简体   繁体   English

验证电子邮件地址

[英]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.

相关问题 使用JavaScript或JQuery验证电子邮件地址 - Validating E-Mail address using JavaScript or JQuery JavaScript:形式:在发送之前验证电子邮件地址并使用Starts.With检查另一个字段 - JavaScript: Form: Validating e-mail address and checking another field with Starts.With before sending jquery包装一个电子邮件地址 - jquery wrap a e-mail address 如何使用正则表达式在javascript中验证PHP电子邮件注入的电子邮件地址? - How to validate an e-mail address against php e-mail injection in javascript using regex? 在“联系人”表单中输入无效的电子邮件地址后,电子邮件跨度下降 - e-mail span drops down after entering invalid e-mail address in the 'contact' form 在网站上混淆电子邮件地址的最佳方法? - Best way to obfuscate an e-mail address on a website? 电子邮件地址的jQuery正则表达式验证,包括空白值 - jQuery regex validation of e-mail address including blank value 需要使用帮助将表单数据发送到电子邮件地址 - Need help sending form data to an e-mail address using JavaScript:提取电子邮件地址的一部分并分成几部分 - JavaScript: Extract parts of e-mail address and split into parts 从已登录的Google地图获取电子邮件地址 - get the e-mail address from signed-in google maps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM