JavaScript不验证我的表单

时间:2014-07-24 11:19:26

标签: javascript

<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;
}

    <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脚本的新手,目前正在学习它。 我的浏览器只验证电子邮件字段并跳过其余字段 为什么这是我的括号是正确的。并且我已经提到仅在最后一个字段之后返回true。

1 个答案:

答案 0 :(得分:0)

(更新)

表单(请注意“return 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()而不是forms []:*

 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;
}