javascript - Jquery formvalidation required field if value is 0 -
i have jquery form validation plugged in page, have issue trying validate state dropdown, how view built this. user chooses country fires controller , returns states linked country , builds states dropdown. before choosing country state dropdown has selected value of 0 , text "please choose state" understanding jquery form validation see 0 , assume value, instead in case not value need user choose greater value of 0. when user presses submit, if states selected value 0 need display error message otherwise carry on. have
$('#myform').validate({ // initialize plugin rules: { "userdetails.selectedcountry": "required", "userdetails.selectedstate": { required: function (element) { var value = $("#userdetails_selectedstate").val() == 0; if (value) { // if value true need return false make required alert("here"); return false; } else { // otherwise value greater 0 ok carry on, return true; } } } "userdetails.postcode": "required" }, messages: { "userdetails.selectedcountry": "please choose country", "userdetails.selectedstate": "please choose state", "userdetails.postcode": "please enter postcode", } });
would please able tell/show how validate against 0.
** update **
i've been battling on , off today, can see jquery markup if true return false see alert message "here" field not highlight in red, nor error message "please choose state" shown, can thing of top of there head be?
you may put "please choose state" value ="" , disabled it.
something this
<select> <option value="" disabled>please choose state</option> <option value="1">1</option> </select>
change validation this:
var value = $("#userdetails_selectedstate").val() if (value ==0 || value=="0") { // if value true need return false make required alert("here"); return false; } else { // otherwise value greater 0 ok carry on, return true; }
Comments
Post a Comment