2017-09-25 45 views
2

这是我的代码:验证不正常使用

function email() { 
 
    var reg = new RegExp("^[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$"); 
 

 
    var nam = document.registration.email.value; 
 
    var res = nam.match(reg); 
 
    if (res) { 
 
    alert("enter valid email"); 
 
    document.registration.email.focus(); 
 
    } else { 
 
    document.registration.password.focus(); 
 
    } 
 
} else { 
 
    document.registration.email.focus(); 
 
} 
 
}
<form name="registration" action="" method="post"> 
 
    <input type="text" name="username" placeholder="Username" required /> 
 
    <input type="text" name="email" placeholder="Email" onblur="email()" required /> 
 
    <input type="password" name="password" placeholder="Password" required /> 
 
    <input type="submit" name="submit" value="Register" /> 
 
</form>

验证不工作,因此在alert条件if没有显示。任何人都可以帮助我实现这种验证。

在此先感谢

+1

你有一个额外的'else' – gurvinder372

+1

使用jQuery的验证,它是比这更容易。由于您必须一直提醒验证消息,因此jquery可以一次完成。如果你仍然想要这个,请参考https://www.w3schools.com/js/js_validation.asp –

+0

你有一对多的'}',你有一个' – Olian04

回答

0

由于您使用HTML 5,你不需要写自己的验证电子邮件只使用 HTML5已经内置的电子邮件验证检查。

<form name="registration" action="" method="post"> 
<input type="text" name="username" placeholder="Username" required /> 
<input type="email" name="email" placeholder="Email" onblur="email()" required /> 
<input type="password" name="password" placeholder="Password" required /> 
<input type="submit" name="submit" value="Register" /> 
</form> 

,但如果你想用你的功能,反正用它作为:

<html> 
<head> 
<script> 
function emails() 
    { 
      var reg=new RegExp("^[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$"); 
       var nam=document.registration.email.value; 
       if(!new RegExp(reg).test(nam)) 
       { 
         alert(document.registration.email); 
      document.registration.password.focus(); 
       } else { 
         document.registration.password.focus(); 
       } 
    } 

</script> 
</head> 
<body> 
<form name="registration" action="" method="post"> 
<input type="text" name="username" placeholder="Username" required /> 
<input type="text" name="email" placeholder="Email" onblur="javascript:emails()" required /> 
<input type="password" name="password" placeholder="Password" required /> 
<input type="submit" name="submit" value="Register" /> 
</form> 
</body> 
0

嗯......假设你正在尝试做一些输入验证您的形式,我建议阅读关于位电子邮件验证正则表达式。然后使用类似的东西: https://www.regextester.com/19

然后,我认为你的if语句是有缺陷的。我想你的意思是,如果电子邮件匹配正则表达式,如果应该专注于密码字段。如果电子邮件不是空的,并且如果应该显示警报则无效。如果电子邮件为空,则应重点关注电子邮件。我做了一个快速清理和我认为的代码应该是这个样子(未测试的代码只是为了说明):

function validateInput() { 
    var email= new RegExp("^[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$"); 
    var inputValue=document.registration.email.value; 
    if(inputValue.match(email)) { 
     document.registration.password.focus(); 
    } else if (inputValue.length > 0) { 
     alert("enter valid email"); 
     document.registration.email.focus(); 
    } else { 
     document.registration.email.focus(); 
    } 
} 
0

电子邮件是在JavaScript保留关键字。首先重命名您的功能电子邮件测试或任何你想要的。第二件事你在代码中还有其他的东西。

0

在你的javascript语法和dom api中有几个错误。

如果你想做手动验证,这里是一个可以工作的小提琴的例子。

https://jsfiddle.net/xb4qrvmy/

function validate_email() 
{ 
    var reg=new RegExp("^[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$"); 

    var nam=document.forms["registration"].email.value; 
    var res=nam.match(reg); 
    if(!res && nam.length) 
    { 
    // I would advice against using alert. 
    alert("enter valid email"); 
    document.registration.email.focus(); 
    // You want to somehow reset the displaying of the error. 
    document.forms["registration"].email.value = '' 
    } else if (res) { 
    document.registration.password.focus(); 
    } 
} 
0
function validate() 
      { 
         var x = document.forms["myform"]["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"); 
          return false; 
         } 
      } 
      </script>