表单验证JavaScript-不显示警报
Form Validation JavaScript - No Alert Appearing
我在表单的表单验证方面遇到了问题。我四处搜索并尝试了一些方法,但没有找到解决方案。我还在学习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
应为true
- 您没有关闭
form
标签 - 没有
submit
按钮 - 您将字符串的长度与
""
进行比较,而不是与数字进行比较
但主要的是,你试图取一个input
(name
)的长度,而不是它的value
的长度。
相反:
function validateForm() {
var name = document.getElementById("name");
if (nameValid(name)) {
return true;
}
return false;
}
function nameValid(name) {
var name_length = name.value.length;
if (name_length == 0) {
alert("Name is required");
name.focus();
return false;
}
return true;
}
<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>
<input type="submit">
</form>
</section>
您需要检查name
元素的值,而不仅仅是元素本身。即:
document.getElementById("name").value
您还需要将length
正确地作为数值进行比较,如此更新函数中所示:
function nameValid(name) {
var name_length = name.length;
alert(name_length);
if (name_length < 1) {
alert("Name is required");
//name.focus();
return false;
}
return true;
}
Fiddle在这里包括一个额外的提醒:http://jsfiddle.net/799vymrz/
(尽管对于正在调试的值,console.log
通常比alert
更有帮助;您需要查看浏览器控制台才能看到console.log
的输出)
相关文章:
- 表单值未在单击按钮时发布,显示验证错误
- 如何使用 javascript 警报窗口显示验证消息
- 如何在不使用javascript和asp.net选择dropdownlist值的情况下显示验证
- 尝试使用Angular.js显示验证消息时出错
- Javascript表单验证-如何在单个表中显示验证错误,而不是使用警报
- 如何一起显示验证消息
- 推送提交后,如果输入字段为黑色,则显示验证检查或以红色字母显示警告
- Laravel 5.2:使用 ajax 提交表单时如何显示验证错误
- 在文本框中键入任何内容之前,将显示验证错误消息
- 为什么不'我的表格没有显示验证错误
- 隐藏/显示验证表单问题
- 在文本输入键上,我想在没有任何插件的情况下显示验证错误
- 当minDate更改/动态时,Md日期选择器未显示验证错误
- XPages-在使用Dojo时显示验证消息
- 如何使用jQuery在每个文本框下面显示验证消息
- Angular x-editable setError不显示验证错误
- 在验证后滚动页面,显示验证弹出框
- Javascript在文本框后面显示验证消息
- 在Jquery中正确显示验证结果
- 当需要该字段时,AngularJS不会显示验证消息