答案 0 :(得分:0)
您考虑使用HTML属性吗?在输入字段上,您可以设置required
属性,并且无法将字段形式发送到其填充状态。另一种选择是在标记上使用patter属性并使用正则表达式来测试验证
http://www.w3schools.com/tags/att_input_required.asp http://www.w3schools.com/tags/att_input_pattern.asp
看看你的代码,在js上,你需要采取一种方式":"在" false"之后并用";"
替换它同样在标记中,您从未在表单上调用该函数,这就是为什么您从未运行该函数的原因。试试这个:
<强> HTML 强>
<form method="post" action="#">
<h3 class="formHeader">Form Header</h3>
<div class="formPod">
<fieldset role="presentation">
<div class="formRow clearfix">
<span><label for="customerName">Your Name</label></span>
<input name="customerName" id="customerName" type="text" size="60" maxlength="200" />
</div>
<div class="formRow clearfix">
<span><label for="customerEmail">Your Email</label></span>
<input name="customerEmail" id="customerEmail" type="text" size="60" maxlength="200" />
</div>
<div class="formRow clearfix">
<fieldset>
<legend>Is the new page</legend>
<div>
<input type="radio" name="radio" id="radioEasier" value="easier" />
<span><label for="radioEasier">Easier to use</label></span>
</div>
<div>
<input type="radio" name="radio" id="radioHarder" value="harder" />
<span><label for="radioHarder">Harder to use</label></span>
</div>
<div>
<input type="radio" name="radio" id="radioSame" value="same" />
<span><label for="radioSame">About the same</label></span>
</div>
</fieldset>
</div>
<div class="formRow clearfix">
<span>
<label for="comments">Comments</label>
<label for="comments" id="charsLeft">(300)</label>
</span>
<textarea name="comments" id="comments" rows="4" cols="40"></textarea>
</div>
</fieldset>
<div class="actionRow clearfix">
<div class="btn">
<input type="submit" value="Submit" onclick="checkForBlank();" />
</div>
</div>
</div>
<强> JS 强>
<script type="text/javascript">
function checkForBlank(){
if (document.getElementById('customerName').value === ""){
alert('Please enter a name');
return false;
}
}
</script>