0

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 are missing the equals sign, =, on all value attributes... value"0" is not correct. The broken syntax is even highlighted in your jsFiddle. Commented Jul 27, 2014 at 13:48

2 Answers 2

1

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/

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

4 Comments

Sure You Right! please see my last update: i add id="test" to each select box. and i have this problem:This field is required. error not show in last select box.jsfiddle.net/hTPY7/1439
@user27133, I have no idea what you're talking about. "This field is required" is properly shown on everything in my demo!
your demo not complete! see this demo : jsfiddle.net/hTPY7/1439
@user27133, Your demo is broken because you've duplicated the same id. id must be unique. In this case, you don't even need id... see my original demo.
0

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

7 Comments

And nobody noticed the missing = in here: value"0"
@Khalid: add missing = and id to select box. your code so not worked. jsfiddle.net/hTPY7/1435
@Khalid: jsfiddle.net/hTPY7/1430 not work. you see :This field is required. error in last select box?
@user27133 Check this jsFiddle jsfiddle.net/hTPY7/1437 and check my post to see the update
@Khalid: in last select box you not add id="test1" and This field is required. error not show in last select box.jsfiddle.net/hTPY7/1439
|

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.