JavaScript中的电子邮件验证错误

时间:2013-02-02 20:02:23

标签: javascript

我是javascript&的新手无法理解使用JS的电子邮件验证。我试了一下但结果却出现了一些错误。我的代码如下,请帮帮我! 希望得到积极的结果!

function validEmail()
{
var email= document.forms["validation"]["enteremail"].value;
var reg = (/^[0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
if (reg.test(email) == false )
  {
  alert("Not a valid e-mail address");
  return false;
  } 
    }

2 个答案:

答案 0 :(得分:1)

试试这个,它对我来说很好......

<script type="text/javascript">
 function ShowAlert() {
  var email = document.getElementById('txtEmailId');
  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;
    }
    else {
        alert("Thanks for your intrest in us, Now you 
        will be able to receive monthly updates from us.");
        document.getElementById('txtEmailId').value = "";
    }
 }
 </script> 

答案 1 :(得分:0)

尝试:

function validEmail()
{
var mail = document.getElementById('mail').value; // where `mail` is id of your input form
var email = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

  if (mail.match(email))
  {
      alert("Your Email : " + mail);
  }
  else
  {
      alert("Invalid Email !!!");
  }
}

HTML:

<input type="text=" id="mail"><input type="button" id="k" value="check" onclick="validEmail()">

Note:当您支持无效的电子邮件时,它会提醒Invalid Email !!!,否则Your Email : example@gmail.com