简体   繁体   English

验证不正确时,输入形式的Javascript检查电子邮件地址仅在Safari中循环

[英]Javascript check e-mail address in input form just loops in Safari when validation is not correct

I have a form and the user needs to enter their e-mail address in one input box. 我有一张表格,用户需要在一个输入框中输入他们的电子邮件地址。 I have a javascript function that is stored in an external file which is being loaded in the head of the html file. 我有一个javascript函数,该函数存储在外部文件中,该外部文件正在html文件的头部加载。

I get a problem with Safari (6.0.5) when I enter a non-valid e-mail address. 输入无效的电子邮件地址时,Safari(6.0.5)出现问题。 The Alert window pops up and when I click OK it just pops up over and over again. 弹出“警报”窗口,当我单击“确定”时,它会一遍又一遍地弹出。 I never get the chance to enter a correct e-mail address. 我从来没有机会输入正确的电子邮件地址。

This is how my function looks like: 这是我的函数的样子:

   function validateEmail()
{
var x=document.forms["theForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  document.theForm.email.focus();
  return false;
  }
}

And I call this function with as seen below: 我用如下所示调用此函数:

<input type="text" name="email" onblur="validateEmail();" /></td >

Use .removeFocus() then you dont have the problem anymore. 使用.removeFocus()那么您就不再有问题了。

function validateEmail()
{
var x=document.forms["theForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  document.getElementById('alert_mail').innerHTML  = "Please change here";
  return false;
  }
    document.getElementById('alert_mail').innerHTML  = "";
}

EDIT: The return false; 编辑: return false; in this function has no effect, I assume the code you posted is not the whole code, anyway return false; 在此功能中没有任何作用,我假设您发布的代码不是全部代码,反正return false; is useful when you submit a form and you wan't to stop submitting if validation does not pass. 在提交表单时非常有用,如果验证未通过,您将不停止提交。

认为您在使用onblur的野生动物园中可能存在一个错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM