我在表單驗證時遇到了麻煩。我已經搜索過,並嘗試了一些東西,但還沒有找到解決方案。我仍然在學習JavaScript,所以我相信這是錯的。以下是我的代碼。表單驗證JavaScript - 無警示顯示
HTML
<section>
<p>Please type in your information so I can send you the newsletter.<br>
Required values are marked by an asterisk (*)</p>
<form id="newsletter" onsubmit="return validateForm()" method "post">
<fieldset id="personal">
<legend>Personal Information</legend>
<label for="name">Name: *</label>
<input type="text" name="name" id="name" size="40">
<br>
<label for="eMail">E-mail Address: *</label>
<input type="text" name="eMail" id="eMail">
<br>
<label for="favBeer">Favorite Beer Style:</label>
<select name="favBeer" id="favBeer">
<option value="IPA">IPA</option>
<option value="Saison">Saison</option>
<option value="Porter">Porter</option>
<option value="Pilsner">Pilsner</option>
<option value="Hefeweizen">Hefeweizen</option>
<option value="Stout">Stout</option>
<option value="Other">Other</option>
</select>
<br>
<label for="comments"> Additional Information:<label>
<textarea name="comments" id="comments" cols="55" rows="5"></textarea>
</fieldset>
</section>
的JavaScript
function validateForm() {
var name = document.getElementById("name");
if(nameValid(name)){
return true;
}
return false;
}
function nameValid(name) {
var name_length = name.length;
if (name_length == "") {
alert("Name is required");
name.focus();
return false;
}
return ture;
}
我注意到的第一件事是,你可能會返回'ture'。 – Marc