2

I am using jQuery multi-select drop down box. Because of it's default property I am unable to validate it with it's name attribute. So that I would like to validate with id. I am using this as reference. But my idea is not working. I don't know where my mistake is??

HTML

<form id="form1" method="post" action="">
    <div>
        <select name="offer" id="offer1">
        <option value="">Select one</option>
        <option value="aaaaa">aaaaaaaaaaa</option>
        <option value="bbbbb">bbbbbbbbbbb</option>
        <option value="cccccc">ccccccccccc</option>
        </select>
    </div>
</form>
<input type="submit" id="submit1" value="Save" /> 

JS

$(function () {
    $("#submit1").click(function () {
        alert("submit")
        var $off = $("#offer1").attr("name");
        var $params = {
            debug: true,
            rules: {},
            messages: {}
        };
        $params['rules'][$off] = "off";
        $params['messages'][$off] = "Select an offer";

        $("#form1").validate($params);
    });
});

fiddle

Sparky
  • 98,165
  • 25
  • 199
  • 285
Sam
  • 5,040
  • 12
  • 43
  • 95

4 Answers4

2

Try this

HTML CODE

<form id="RNA" name="RNA">
  <select size="5" name="sampleMut[]" multiple="multiple" id="sampleMut">
   <option value="41" >41</option>
   <option value="48" >48</option>
   <option value="65" >65</option>
   <option value="102" >102</option>
  </select>
  <input type="submit" />
</form>

JavaScript CODE

$(function(){
$('form').submit(function(){
     var options = $('#sampleMut > option:selected');
     if(options.length == 0){
         alert('no value selected');
         return false;
     }
 });
});

You can select multiple value from this select. options.length gives you how many values you select.

Prateek
  • 6,785
  • 2
  • 24
  • 37
0

enclose <option> tags inside your <select> tag

<select name="offer" id="offer1>
    <option value="">Select one</option>
    <option value="aaaaa">aaaaaaaaaaa</option>
    <option value="bbbbb">bbbbbbbbbbb</option>
    <option value="cccccc">ccccccccccc</option>
</select>
gherkins
  • 14,603
  • 6
  • 44
  • 70
0

Please remove the [ ] in adding rule and message

The answer is:

$(function () {
    $("#submit1").click(function () {
        alert("submit");
        var $off = $("#offer1").attr("name");
        var $params = {
            debug: true,
            rules: {},
            messages: {}
        };

       //// Please note here
          $params.rules [$off] = "required";    
           $params.messages[$off] = "Select an offer";

      //
        $("#form1").validate($params);
    });
});
Abdul Razak
  • 253
  • 2
  • 3
  • 14
  • Don't add multiple ans. You can edit your first ans if you want to do some change. – Arpit Oct 25 '13 at 09:33
  • -1, You absolutely do not need to put `.validate()` inside of a `click` handler. The `.validate()` call is the **one-time** _initialization_ method and therefore does not need to be repeatedly called on button `click`. The click event of the submit button is automatically captured by the plugin. I know you're just copying the OP's code, however, if you're going to post an answer, it might as well promote the proper usage. @Sam, see: http://jsfiddle.net/BzSLC/ – Sparky Oct 25 '13 at 20:50
-1

Try out this:

$(function () {

 $("#submit1").click(function(){
 alert("submit");
 $("#form1").validate({
        rules: {
            offer: "required"

        },
        messages: {
            offer: "Select an Offer"

        }
    });
 });
});
Abdul Razak
  • 253
  • 2
  • 3
  • 14
  • -1, You absolutely do not need to put `.validate()` inside of a `click` handler. The `.validate()` call is the **one-time** _initialization_ method and therefore does not need to be repeatedly called on button `click`. The click event of the submit button is automatically captured by the plugin. See: http://jsfiddle.net/BzSLC/ – Sparky Oct 25 '13 at 20:52