简体   繁体   中英

Jquery validation not validate multi select box in form

I have two select box(dropdpwn) in my page. i validate my form using jQuery validation plugin but this plugin not validation two select box.

JS:

$('form').validate({
    rules: {
        firstname: {
            minlength: 3,
            maxlength: 15,
            required: true
        },
        lastname: {
            minlength: 3,
            maxlength: 15,
            required: true
        }
    },
    highlight: function(element) {
        $(element).closest('.form-group').addClass('has-error');
    },
    unhighlight: function(element) {
        $(element).closest('.form-group').removeClass('has-error');
    },
    errorElement: 'span',
    errorClass: 'help-block',
    errorPlacement: function(error, element) {
        if(element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        } else {
            error.insertAfter(element);
        }
    }
});

HTML :

<form>
    <div class="form-group">
        <label class="control-label" for="firstname">Nome:</label>
        <div class="input-group">
            <span class="input-group-addon">$</span>
            <input class="form-control" placeholder="Insira o seu nome próprio" name="firstname" type="text" />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label" for="lastname">Apelido:</label>
        <div class="input-group">
            <span class="input-group-addon">€</span>
            <input class="form-control" placeholder="Insira o seu apelido" name="lastname" type="text" />
        </div>
    </div>
    <div class="form-group">
        <label class="control-label" for="lastname">selectBox 1</label>
        <div class="input-group">
            <span class="input-group-addon">€</span>
           <select class="required form-control">
               <option value="0"></option>
               <option value="1">1</option>
               <option value="2">2</option>

            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label" for="lastname">selectBox 2</label>
        <div class="input-group">
            <span class="input-group-addon">€</span>
           <select class="required form-control">
               <option value="0"></option>
               <option value="1">1</option>
               <option value="2">2</option>

            </select>
        </div>
    </div>

        <button type="submit" class="btn btn-primary">Submit</button>
</form>

How do can fix this problem for each() selectbox.

DEMO : http://jsfiddle.net/hTPY7/1435

1) You must have a unique name attribute on each element. This is a requirement of the jQuery Validate plugin.

2) The first option must have value="" and not value="0" .

Working DEMO: http://jsfiddle.net/hTPY7/1436/


3) OP's demo is broken because he keeps using the same id twice. For valid HTML, every id must be unique on a page.

DEMO with ID: http://jsfiddle.net/hTPY7/1441/

All you need to do is to add the name attribut to both select boxes and set the first option to no text and no value

<select name="s1" class="required form-control">
              ^--  Don't forget to add different names for both select
    <option></option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

jsFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM