[英]Javascript form validation doesn't work
我知道這很愚蠢,但是標記之間的代碼卻無法正常工作。
我有一個簡單的HTML注冊表格。 然后,js代碼會簡單檢查所有輸入是否為空。
function validate()
{
if (document.forms.registration.email.value == "")
{
alert("You must provide an email address.");
return false;
}
else if (document.forms.registration.password1.value != document.forms.registration.password2.value)
{
alert("You must provide the same password twice");
return false;
}
else if (!document.forms.registration.agreement.checked)
{
alert("You must agree to ours terms and conditions");
return false;
}
return true;
}
由於某種原因,瀏覽器會忽略此代碼並提交表單,即使它為空。
編輯:(基於OP注釋添加)
<form action="process.php" id="registration" method="get" onsubmit="return.validate();">
<!-- Other child elements of the form -->
</form>
替換為onsubmit="return.validate();"
通過onsubmit="return validate();"
。
正如@showdev所說的那樣:
您不需要點。
您還可以使用jQuery之類的JavaScript庫來生成代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.