0

Is it possible, using jquery validation, to set required to a Bootstrap styled div dropdown, vs. option/select? I can't change the tags to option/select on this page. There are also multiple div dropdowns.

<div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
        <span data-bind="label">Month</span><span class="caret"></span>
        <label class="error" for="dobMonth" generated="true"></label>
     </button>
     <ul name="dobMonth" id="dobMonth" class="dropdown-menu">
         <li><a href="#">January</a></li>
         <li><a href="#">February</a></li>
         <li><a href="#">March</a></li>
         <li><a href="#">April</a></li>
     </ul>
 </div>


$(document).ready(function () {
    $("#form").validate({
        rules: {
            "dobMonth": {
                required: true
            }
        },
        messages: {
            "dobMonth": {
                required: "Enter the month you were born"
            }
        },
        submitHandler: function (form) {
            alert('valid form submitted');
            return false;
        }
    });
});

http://jsfiddle.net/sw87W/24/

1
  • You should check the answer given here: Jquery Validate Commented Sep 29, 2016 at 2:56

1 Answer 1

0

Why don't create your own custom validation for it? You can't use jQuery validation if you're using button and ul. Ul doesn't have a name attribute. So, there's no value inside of it. The trick here is check the span innerHTML if it's equal to 'Month'.

Here's a sample code for it.

$('#form').on('submit', function(e){
    e.preventDefault();
  var mo = $('#id-month').html();

  mo == 'Month' ? alert('add error') : alert('remove error');
})

Look at it on fiddle. http://jsfiddle.net/sw87W/26/

It's up to you if you create a prototype function to make it dynamic. You can just simply append an element with a class of (error, label-error, or something). And remove it if it's false. I used jQuery validator before. Hope someone can provide a solution for you using validator. Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.