2014-07-24 59 views
0
<script> 
function validateForm() { 
var x = document.forms["Email"].value; 
var atpos = x.indexOf("@"); 
var dotpos = x.lastIndexOf("."); 
if (atpos<1 || dotpos<atpos+2 || dotpos+2>x.length || x="" || x=str.search(" ");) 
{ 
     alert("Not a valid e-mail address!!"); 
     return false; 

} 
var y = document.forms["Password"].value; 
if(y.length>6) 
else 
{ 
     alert("Minimum 6 Characters Required!!"); 
     return false; 

} 
var vl = document.forms["Phno"].value; 
for (var i = 0; i<vl.length; i++) 
{ 
    if((vl[i]!="a" && vl[i]!="b" && vl[i]!="c" && vl[i]!="d" && vl[i]!="e"    && vl[i]!="f" && vl[i]!="g" && vl[i]!="h" 

&& vl[i]!="i" && vl[i]!="j" && vl[i]!="k" && vl[i]!="l" && vl[i]!="m" && vl[i]!="n" &&  vl[i]!="o" && vl[i]!="p" && vl[i]!="q" && 

vl[i]!="r" && vl[i]!="s" && vl[i]!="t" && vl[i]!="u" && vl[i]!="v" && vl[i]!="w" &&       vl[i]!="x" && vl[i]!="y" && vl[i]!="z" && vl 

    [i]!="@" && vl[i]!="_" && vl[i]!="." && vl[i]!="-")) 
    { 

     alert("Enter a valid Phone Number!!"); 
     return false; 
    } 
} 
if(vl.lenght>10 || vl.lenght<10) 
{ 
    alert("Enter a valid Phone Number!! "); 
    return false; 
} 

var d = document.forms["date"].value; 
if(d="") 
return false; 
else 
return true; 
} 

的JavaScript沒有驗證我的形式

<form method="post" onsubmit="validateForm()"> 
      <center> <input style="padding: 5 10px;width:274px;height:44px;margin-bottom:                  10px;" id="Email" name="Email" 

type="email" placeholder="Email"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="Password" name="Password" 

type="Password" placeholder="Password"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="Phno" name="Phno" 

type="phonenumber" placeholder="Phone No"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="date" name="date" type="date" 

placeholder="DOB"> 
        <button type="submit" style="border: 1px solid; background-color:#3079ed; width:274px;height:44px;" 

class="button"><b>Sign Up</b></button>  
</center> 
</form> 

我是新來的Java腳本,並正在學習它。 我的瀏覽器只驗證電子郵件字段並跳過該字段的其餘部分 爲什麼這是我的括號正確。而且我提到只有在最後一場之後纔會回到真實。

+0

這是因爲您在電子郵件狀態下返回false if(atpos <1 || dotpos x.length || x =「」|| x = str.search(「」);) { alert(「Not a valid e-mail address !!」); 返回false; //我在說這個 } –

+0

你應該看看使用正則表達式來驗證字符串,例如'/^[a-z @_。。] + $/i.test(vl)'可能代替所有那些'||'表達式。 – RobG

+0

你有太多的錯誤,我會糾正併發布:) – Youness

回答

0

(UPDATE)

表格(注意 「迴歸validateForm();」 而不是 「validateForm()」:

<form method="post" onsubmit="return validateForm();"> 
      <center> <input style="padding: 5 10px;width:274px;height:44px;margin-bottom:                  10px;" id="Email" name="Email" 

type="email" placeholder="Email"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="Password" name="Password" 

type="Password" placeholder="Password"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="Phno" name="Phno" 

type="phonenumber" placeholder="Phone No"> 
      <input style="padding: 5 10px;width:274px;height:44px;margin-bottom: 10px;" id="date" name="date" type="date" 

placeholder="DOB"> 
        <button type="submit" style="border: 1px solid; background-color:#3079ed; width:274px;height:44px;" 

class="button"><b>Sign Up</b></button>  
</center> 
</form> 

*的函數注意GetElementById()代替形式[]:*

function validateForm() { 
var x = document.getElementById("Email").value; 
var atpos = x.indexOf("@"); 
var dotpos = x.lastIndexOf("."); 
if (atpos<1 || dotpos<atpos+2 || dotpos+2>x.length || x==="" ||x.indexOf(" ")>=0) 
{ 
     alert("Not a valid e-mail address!!"); 
     return false; 

} 
var y = document.getElementById("Password").value; 
if(y.length<6) 
{ 
     alert("Minimum 6 Characters Required!!"); 
     return false; 

} 
var vl = document.getElementById("Phno").value; 
for (var i = 0; i<vl.length; i++) 
{ 
    if(isNaN(vl[i])) 
    { 
     alert("Enter a valid Phone Number!!"); 
     return false; 
    } 
} 
if(vl.lenght!=10) 
{ 
    alert("Enter a valid Phone Number!! "); 
    return false; 
} 

var d = document.getElementById("date").value; 
if(d==="") 
return false; 

return true; 
} 
+0

沒有仍然跳過其餘的字段 – Nik6019

+0

我會看到只是等待 – Youness

+0

我更新了現在可以測試它的功能 – Youness