jQuery validate select box

后端 未结 6 1252
孤独总比滥情好
孤独总比滥情好 2021-01-03 21:36

I am using jQuery Validation Plugin for form validation and I am not able to validate list/select box


                        
    
提交评论

  • 2021-01-03 22:16

    Why don't you use the value of the condition?

    required: function(elm) {
        return (elm.options[elm.options.selectedIndex].value != "-1");
    }
    

    This does the same as the solution from Robin, but is much shorter.

    0 讨论(0)
  • 2021-01-03 22:20

    You can use Jquery Validator custom rule.

    A similar example is discussed here. The code below is from the example:

    // add the rule here
     $.validator.addMethod("valueNotEquals", function(value, element, arg){
      return arg != value;
     }, "Value must not equal arg.");
    
     // configure your validation
     $("form").validate({
      rules: {
       SelectName: { valueNotEquals: "-1" }
      },
      messages: {
       SelectName: { valueNotEquals: "Please select an item!" }
      }  
     });
    
    0 讨论(0)
  • 2021-01-03 22:20

    Put <option value="">Choose an option</option> as blank rather than -1

    using jquery validation

    rules: {                      
                    select: "required"
    }
    
    0 讨论(0)
  • 2021-01-03 22:23

    Or use the element value:

    required: function(elm) {
        if(elm.options[elm.options.selectedIndex].value != "-1") {
            return true;
        } else {
            return false;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 22:38

    How can we validate for jquery ui select dropdown

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script> 
    <script>
    $(".intselect").selectmenu();
    $().ready(function() {
    
    
    jQuery.validator.setDefaults({
      debug: true,
      success: "valid"
    });
    $( "#validateregister" ).validate({
      rules: {
    	  firstname: {
          required: true
        },
    	 email: {
          required: true,
    	  email: true
        },
        year: {
          required: true
        }
      }
      
    });
    $('#validateregister').change(function() {
            if ($("#validateregister").valid()) {
                $(".go").removeAttr("disabled");
            }
    		else{
    			 $(".go").attr("disabled","disabled");
    		}
        });
    });
    </script>
    "sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <form id="validateregister" style="width: 300px;">
      <div> <br />
        <input type="text" name="firstname" class="form-control" id="firstname" />
      </div>
      <div> <br />
        <input type="text" name="email" class="form-control" id="email" />
      </div>
      <div> <br />
        <select class="form-control intselect" name="year" id="year">
          <option value="">Select year</option>
          <option value="2014">2014</option>
          <option value="2015">2015</option>
          <option value="2016">2015</option>
        </select>
      </div>
      <div> <br />
        <input type="submit" class="btn btn-default go" value="submit" disabled>
      </div>
    </form>

    0 讨论(0)
  • 提交回复
    热议问题