<form action="${ pageContext.request.contextPath }/addProduct" method="post" onsubmit="return checkform()">
商品名称:<input type="text" name="pname" id="pname"/><br/>
市场价格:<input type="text" name="market_price" /><br/>
商城价格:<input type="text" name="shop_price" /><br/>
添加时间:<input type="text" name="pdate" id="pdate"/><br/>
商品描述:<textarea rows="5" cols="15" name="pdesc"></textarea><br/>
<input type="submit" value="添加商品" />
</form>
<script type="text/javascript">
// 校验表单
function checkform(){
var pname = document.getElementById("pname").value;
if(pname == ""){
// 说明没有输入商品名称
alert("商品名称不能为空");
return false;
}
}
</script>
更详细的内容:http://www.w3school.com.cn/js/js_form_validation.asp
表单校验--用户是否已填写表单中的必填项目
原文:http://blog.51cto.com/13579086/2074307