如何验证JavaScript中的电子邮件地址? [英] How to validate email address in JavaScript?

查看:147
本文介绍了如何验证JavaScript中的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

使用正则表达式可能是最好的方法。您可以看到一些测试 here (取自

  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);
}

以下是接受unicode的常规表达式的示例:

  var re = /^(([^<>()\[\]\.,;:\s@\\ \\ ] +(。\ [^<>()\ [\] \。,;:\s @ \] +)*)|(\ + \ ))@(([^<>()[\] \。,;:\s @ \]。+ \)+ [^<>()[\] \\ \\;:\s @ \] {2,})$ / I; 

但请记住,不应仅依靠JavaScript验证。 JavaScript可以很容易地被禁用。这也应该在服务器端进行验证。



以下是上述操作的示例:



div class =snippetdata-lang =jsdata-hide =falsedata-console =truedata-babel =false>

  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);} function validate(){$(#result)。text(); var email = $(#email)。val(); if(validateEmail(email)){$(#result)。text(email +is valid :)); $(#result)。css(color,green); } else {$(#result)。text(email +not valid :(); $(#result)css(color,red);} return false;} $ #validate)。bind(click,validate);  

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< form>< p>输入电子邮件地址:< / p>< input id ='email'>< button type ='submit'id ='validate'>验证!< / button>< / form>< h2 id ='result'>< / h2>  


How can an email address be validated in JavaScript?

解决方案

Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium)

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);
}

Here's the example of regular expresion that accepts unicode:

var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

Here's an example of the above in action:

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);
}

function validate() {
  $("#result").text("");
  var email = $("#email").val();
  if (validateEmail(email)) {
    $("#result").text(email + " is valid :)");
    $("#result").css("color", "green");
  } else {
    $("#result").text(email + " is not valid :(");
    $("#result").css("color", "red");
  }
  return false;
}

$("#validate").bind("click", validate);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <p>Enter an email address:</p>
  <input id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

<h2 id='result'></h2>

这篇关于如何验证JavaScript中的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆