2017-03-15 36 views
0

我已经做了emailid和密码验证的示例代码,但无法验证emailid和密码。任何人都可以帮助我解释这一点吗?emailid和密码验证的简单javascript代码

<html> 
     <head> 
      <script language="javascript"> 
       function emailid(){ 
        var a=f.emailid; 
        if(a==" "){ 
         alert("emailid must not be empty"); 
        } 
        else if(a.indexof("@")!=-1){ 
         alert("not valid id"); 
        } 
        else if(a.indexof(".")!=-1){ 
         alert("not a valid id"); 
        } 
        else 
        alert("valid id"); 
       } 
       function pswd(){ 
        var b=f.password; 
        if(b==" "){ 
         alert("password must not be empty"); 
        } 
        else if(b.length<6){ 
         alert("not valid password"); 
        } 
        else 
        alert("vaild password"); 
       } 
      </script> 
      <a href="submit.html"></a> 
     </head> 
     <body> 
      <form> 
       emailid<input type="text" name="emailid"><input type="button" value="validate" onclick="emailid()"><br> 
       password<input type="password" name="password"><input type="button" value="validate" onclick="pswd()"><br> 
       <input type="button" value="submit" onclick="submit.html"> 
      </form> 
     </body> 
    </html> 

另外其它部分submit.html

<html> 
    <body> 
     successfully submitted 
    </body> 
</html> 
+0

可变'F'没有任何地方所定义。 'onclick'不是用于导航到成功页面,而是用于执行JavaScript。 – bejado

回答

0

 <script type="text/javascript"> 
    <!-- 

     function validateEmail() { 
     var f = document.getElementById("myForm").elements;    
      var a=f.emailid.value; 
       if(a==""){ 
        alert("emailid must not be empty"); 
       } 
       else if(a.indexOf("@") ==-1 || a.indexOf(".") ==-1){ 
        alert("not valid id"); 
       } 
       else 
       alert("valid id"); 
     } 

     function pswd(){ 
      var f = document.getElementById("myForm").elements; 
       var b=f.password.value; 
       if(b==""){ 
        alert("password must not be empty"); 
       } 
       else if(b.length<6){ 
        alert("not valid password"); 
       } 
       else 
       alert("vaild password"); 
      }; 
    //--> 
    </script> 
    </head> 
    <body> 
    <a href="submit.html"></a> 
     <form id='myForm'> 
      emailid<input type="text" name="emailid"><input type="button" value="validate" onclick="validateEmail()"><br> 
      password<input type="password" name="password"><input type="button" value="validate" onclick="pswd()"><br> 
      <input type="button" value="submit" onclick="submit.html"> 
     </form> 
    </body> 
</html> 
+0

我刚编辑并更改了它工作的函数的名称 –

+0

该函数正在工作,但仍然提交按钮不起作用。 –