jquery - Validation for select box -
i doing form validation using jquery. not getting how validate select list menu.
could please me out this.
here code , fiddle
$.validator.setdefaults({ submithandler: function() { alert("submitted!"); } }); $().ready(function() { // validate comment form when submitted $("#commentform").validate(); // validate signup form on keyup , submit $("#signupform").validate({ rules: { firstname: "required", lastname: "required", email: { required: true, email: true }, topic: { required: "#newsletter:checked", minlength: 2 }, agree: "required" }, messages: { firstname: "please enter firstname", lastname: "please enter lastname", username: { required: "please enter username", minlength: "your username must consist of @ least 2 characters" }, email: "please enter valid email address", agree: "please accept our policy" } }); });
1) set id
select
tag.
2) create default option empty string value.
<option value="">select option</option>
<select id="country" name="country"> <!--set id--> <option value="">select option</option> <!--default option--> <option value="afghanistan">afghanistan</option> <option value="albania">albania</option> <option value="algeria">algeria</option> <option value="american samoa">american samoa</option> <option value="andorra">andorra</option> <option value="angola">angola</option> <option value="anguilla">anguilla</option> <option value="antarctica">antarctica</option> <option value="antigua & barbuda">antigua & barbuda</option> <option value="antilles, netherlands">antilles, netherlands</option> <option value="arabia, saudi">arabia, saudi</option> <option value="argentina">argentina</option> </select>
in js, add below code in validate
method,
within rule
block
country: { required: true }
within messages
block
country: "please select option"
check working fiddle
fyi: mandatory fields should prefixed or suffixed *
show difference.
hope understand.
Comments
Post a Comment