-4

我是 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;
  } 
    }
4

2 回答 2

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> 
于 2013-10-08T13:05:51.467 回答
0

Try:

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: When you support invalid email it will alert Invalid Email !!! otherwise Your Email : example@gmail.com

于 2013-02-02T20:09:53.163 回答