I'm using jQuery v1.7.1 in my project.
I've following HTML form:
<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>
Now here I want to validate the form with id="formzip"
using jQuery. Following validations need to apply:
- If the input field having id zip doesn't contain any value at the submission of a form.
- If the input field having id zip contains invalid US zip code value or any value which is not a valid US zip code.
I want to show relevant error messages in a <div class="alert-danger"></div>
and want to prevent the form from submission. That is the page currently displaying should remain as it is.
The necessary regular expression string to validate US zip code is as follows :
"/^([0-9]{5})(-[0-9]{4})?$/i"
Can someone please help me in achieving this functionality?
It will be of immense help to me if someone could help me.
thanks for spending some of your valuable time in understanding my issue. If you want any further information regarding my issue please feel free to ask me.
Waiting for your precious replies.
Thanks in advance.