JQuery Validate Dropdown list

前端 未结 7 1813
后悔当初
后悔当初 2020-12-05 13:56

I\'m using the validation plugin from here. I\'m trying force the user to select an option in the drop down list so here\'s my html for the list:

Select the          


        
相关标签:
7条回答
  • 2020-12-05 13:59

    As we know jQuery validate plugin invalidates Select field when it has blank value. Why don't we set its value to blank when required.

    Yes, you can validate select field with some predefined value.

    $("#everything").validate({
        rules: {
            select_field:{
                required: {
                    depends: function(element){
                        if('none' == $('#select_field').val()){
                            //Set predefined value to blank.
                            $('#select_field').val('');
                        }
                        return true;
                    }
                }
            }
        }
    });
    

    We can set blank value for select field but in some case we can't. For Ex: using a function that generates Dropdown field for you and you don't have control over it.

    I hope it helps as it helps me.

    0 讨论(0)
  • 2020-12-05 14:01
        <div id="msg"></div>
    <!-- put above tag on body to see selected value or error -->
    <script>
        $(function(){
            $("#HoursEntry").change(function(){
                var HoursEntry = $("#HoursEntry option:selected").val();
                console.log(HoursEntry);
                if(HoursEntry == "")
                {
                    $("#msg").html("Please select at least One option");
                    return false;
                }
                else
                {
                    $("#msg").html("selected val is  "+HoursEntry);
                }
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-05 14:02

    The documentation for required() states:

    To force a user to select an option from a select box, provide an empty options like <option value="">Choose...</option>

    By having value="none" in your <option> tag, you are preventing the validation call from ever being made. You can also remove your custom validation rule, simplifying your code. Here's a jsFiddle showing it in action:

    http://jsfiddle.net/Kn3v5/

    If you can't change the value attribute to the empty string, I don't know what to tell you...I couldn't find any way to get it to validate otherwise.

    0 讨论(0)
  • 2020-12-05 14:03
    $(document).ready(function(){
    $("#HoursEntry").change(function(){
    var HoursEntry = $(#HoursEntry option:selected).val();
    if(HoursEntry == "")
    {
    $("#HoursEntry").html("Please select");
    return false;
    }
    });
    });
    
    0 讨论(0)
  • 2020-12-05 14:20

    I have modified your code a little. Here's a working version (for me):

        <select name="dd1" id="dd1">
           <option value="none">None</option>
           <option value="o1">option 1</option>
           <option value="o2">option 2</option>
           <option value="o3">option 3</option>
        </select>  
        <div style="color:red;" id="msg_id"></div>
    
    <script>
        $('#everything').submit(function(e){ 
            var department = $("#msg_id");
            var msg = "Please select Department";
                if ($('#dd1').val() == "") {
                    department.append(msg);
                    e.preventDefault();
                    return false;
                }
            });
     </script>
    
    0 讨论(0)
  • 2020-12-05 14:23

    Easiest way to get it done is by adding required into your select

    Select the best option
    <br/>
    
    <select name="dd1" id="dd1" required>
    <option value="none">None</option>
    <option value="o1">option 1</option>
    <option value="o2">option 2</option>
    <option value="o3">option 3</option>
    </select> 
    
    <br/><br/>​
    
    0 讨论(0)
提交回复
热议问题