我想驗證我的表單。我的代碼中有一個表單驗證的javascript代碼,點擊查看器,但它不工作,仍然沒有任何驗證地將值發送到下一頁。您能告訴我我在哪裏我犯了錯誤,爲什麼會發生這種情況?使用JavaScript和html進行表單驗證
代碼:
<form method="get" action="/calculator/stage_two" name="calculate" id="calculate" onsubmit="return validateForm();">
<div class="calc_instruction">
<input type="text" name="property_number" placeholder="No/Name" id = "property_number" class="stage_one_box" />
<input type="text" name="postcode" placeholder="Postcode" id = "postcode" class="stage_one_box" />
</div>
<input type = "image" name = "submit_calculator" id = "submit_calculator" value="Go" src = "/images/next_button.png" />
</form>
JavaScript函數:
<script type="text/javascript">
function validateForm() {
var postcode=document.forms["calculate"]["postcode"].value;
if (postcode==null || postcode=="") {
alert("Please enter the postcode to give you more accurate results");
document.forms["calculate"]["postcode"].focus();
return false;
}
</script>
你得到的是警報? – arjuncc
保持結構清潔非常重要!然後像缺失的括號不會發生錯別字經常再次發生錯別字。 –
如果您使用的是mozilla,請在出現錯誤後按(ctrl + shift + j)。這將有助於您進行調試。 – Shurmajee