-1
我一直在試圖找出這個簡單的代碼。我把它放在IF Firstname爲空或== 1的地方,它會ALERT,但是最終的IF語句總是響起來,並且不會提示false或true。有任何想法嗎?Javascript簡單的窗體驗證不起作用
function validateForm()
{
var firstname=document.getElementById('firstname').value.trim();
var count=0;
if (firstname.length==0)
{ alert("EMPTY");
document.getElementById("err_firstname").innerHTML="<br><span class='errorbar'>*This is a required field</span>";
} else {
count++;
alert("COUNTED");
document.getElementById("err_firstname").innerHTML="";
}
if (count==1)
{
return true;
alert("true");
} else {
document.getElementById("err_allerrors").innerHTML="<br><span class='errorbar'>*There is required information missing. Please enter all required data.</span>";
return false;
alert("false");
}
//Verified
}
想象一下這樣的代碼,而所有警報。它仍然返回true。 –
第一條語句返回EMPTY或COUNTED。我在第二條語句中刪除了兩條警報,但仍然返回TRUE。 –
當你調用你的函數時,count變量可能只增加一次(不再)。所以這就是爲什麼它幾乎返回1 – HICURIN