<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腳本,並正在學習它。 我的瀏覽器只驗證電子郵件字段並跳過該字段的其餘部分 爲什麼這是我的括號正確。而且我提到只有在最後一場之後纔會回到真實。
這是因爲您在電子郵件狀態下返回false if(atpos <1 || dotpos x.length || x =「」|| x = str.search(「」);) { alert(「Not a valid e-mail address !!」); 返回false; //我在說這個 } –
你應該看看使用正則表達式來驗證字符串,例如'/^[a-z @_。。] + $/i.test(vl)'可能代替所有那些'||'表達式。 – RobG
你有太多的錯誤,我會糾正併發布:) – Youness