我在我的项目中使用jQuery v1.7.1。
我有以下 HTML 表单:
<div class="alert-danger">
</div>
<form action="index.php" class="navbar-form pull-right" id="formzip" method="post">
<input type="hidden" class="form-control" name="op" id="op" value="zip_code">
<input type="hidden" class="form-control" name="form_submitted" id="form_submitted" value="yes">
<input style="width: 115px;" type="text" placeholder="Enter the zip code" name="zip" id="zip" value=""> <i class="icon-zip" style="margin-top:3px;" onclick='$("#formzip").submit();'></i>
</form>
现在在这里我想id="formzip"
使用 jQuery 来验证表单。需要申请以下验证:
- 如果具有 id zip 的输入字段在提交表单时不包含任何值。
- 如果具有 id zip 的输入字段包含无效的美国邮政编码值或任何不是有效美国邮政编码的值。
我想在 a 中显示相关的错误消息,<div class="alert-danger"></div>
并希望阻止表单提交。即当前显示的页面应保持原样。
验证美国邮政编码所需的正则表达式字符串如下:
"/^([0-9]{5})(-[0-9]{4})?$/i"
有人可以帮我实现这个功能吗?
如果有人可以帮助我,那对我会有很大的帮助。
感谢您花费宝贵的时间来理解我的问题。如果您想了解有关我的问题的更多信息,请随时问我。
等待您宝贵的回复。
提前致谢。