2

I have multiple forms in my web, you can show/hide them depeding on your query, but the jquery validate just work on the first one (Particular). Here I have my forms: https://www.lokkura.es/contacto2.php clicking on the left panel you will change the form.

Form 1 (jquery validation works here):

      <div id="form1">
            <form id="form" class="form" action="#" method="post">
            <div class="clearfix">

            <div class="col-md-6 text-center">

                <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder="* Nombre : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Nombre :'" />
                </div>

                <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder=" Ciudad : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Ciudad :'" />
                </div>

                <div class="control-group">
                <input name="email" type="text" class="validate['required','email']  textbox1"
                placeholder="* Email : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Email :'"/>
                </div>       

                 <div class="control-group">
                <input name="phone" type="text" class="validate['required']  textbox1"
                placeholder="* Teléfono : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Teléfono :'"/>
                </div> 

            </div>

            <div class="col-md-6 text-center">
                <div class="control-group">
                <textarea name="message" class="validate['required'] messagebox1"
                placeholder="* Mensaje : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Mensaje :'"></textarea>
                </div>
            </div>

            <div class="clearfix">
                <input value="Enviar Mensaje"  name="submit" type="submit" class="submitBtn" />
            </div>
            <div id="post-message-contact" class="post_message"></div>    
            </div>
            </form>
            </div>  

Form 2 (jquery validation does not work here):

      <div id="form2" style="display:none">

                  <form  id="form"  class="form" action="#" method="post">
            <div class="clearfix">

            <div class="col-md-6 text-center">

                <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder="* Nombre : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Nombre :'" />
                </div>

                <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder=" Ciudad : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Ciudad :'" />
                </div>

                <div class="control-group">
                <input name="email" type="text" class="validate['required','email']  textbox1"
                placeholder="* Email : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Email :'"/>
                </div>       

                <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder=" Empresa : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Empresa :'" />
                </div>

                 <div class="control-group">
                <input name="name" type="text" class="validate['required'] textbox1" placeholder=" Web de Empresa : "
                onfocus="this.placeholder = ''" onBlur="this.placeholder = '* Web de Empresa :'" />
                </div>

                 <div class="control-group">
                <input name="phone" type="text" class="validate['required']  textbox1"
                placeholder="* Teléfono : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Teléfono :'"/>
                </div> 

            </div>

            <div class="col-md-6 text-center">
                <div class="control-group">
                <textarea name="message" class="validate['required'] messagebox1"
                placeholder="* Mensaje : " onFocus="this.placeholder = ''" onBlur="this.placeholder = '* Mensaje :'"></textarea>
                </div>
            </div>

            <div class="clearfix">
                <input value="Enviar Mensaje"  name="submit" type="submit" class="submitBtn" />
            </div>
            <div id="post-message-contact" class="post_message"></div>    
            </div>
            </form>
                            </div>

Javascript Code:

jQuery(document).ready(function($){
"use strict";   
  $('#form').validate(
    {
    rules: {
    name: {
    minlength: 2,
    required: true
    },
    phone: {
    required: true,
    },
    email: {
    required: true,
    email: true
    },
    message: {
    minlength: 2,
    required: true
    }
    },
    highlight: function(element) {
    $(element).closest('.control-group').removeClass('success').addClass('error');
    },
    success: function(element) {
    element
    .text('').addClass('valid')
    .closest('.control-group').removeClass('error').addClass('success');
    },
    submitHandler: function(form) {
                    // do other stuff for a valid form
                    $.post('contact_form.php', $("#form").serialize(), function(data) {
                         // action file is here 
                    $('#post-message-contact').html(data);
                    });
                }
    });
    });
9
  • Post code in the question please. Probably duplicate ids. Commented Jul 8, 2014 at 15:17
  • yes, all the forms have the same id, but even changing it, the validaition does not work Commented Jul 8, 2014 at 15:22
  • I don't see any javascript in your posted code. Commented Jul 8, 2014 at 15:25
  • the only JS I'm using is the jQuery Validation Plugin Commented Jul 8, 2014 at 15:38
  • You cannot just have HTML and no JavaScript at all. Where is your call to .validate() at least. Commented Jul 8, 2014 at 15:59

2 Answers 2

5

You are attaching .validate() to the element with id="form"

$('#form').validate({...
  • However, you have two form elements with this same id. You cannot use the same id more than once on a page. Repeating the id is invalid HTML. jQuery will only target the first instance.

  • Even if you used a class instead, the various jQuery Validate methods cannot be attached to selectors that target more than one element at a time. So again, even if your target correctly selected a group of elements, only the first instance would be used.


1) You need to fix the invalid HTML by using a unique id on each form.

<form id="form_1" ...

<form id="form_2" ...

2) You need to construct a selector that targets all elements in this group. You can use a "starts with" selector to grab every id that "starts with" form_.

$('[id^="form_"]')

3) You need to enclose your .validate() within a jQuery .each() so that you can apply it to each instance and not just the first.

jQuery(document).ready(function($){

    $('[id^="form_"]').each(function() { // selects all forms with id starting with "form_"
        $(this).validate({ ....          // call 'validate()' method on each one

Alternatively, your .form class would work here too.

jQuery(document).ready(function($){

    $('.form').each(function() {  // selects all forms with class="form"
        $(this).validate({ ....   // call 'validate()' method on each one
Sign up to request clarification or add additional context in comments.

1 Comment

that works great, the validation works on all the forms, but they are not sent though. I tried to modified this line, changing "#form" into 'this' with no luck: $.post('contact_form.php', $("#form").serialize(), function(data) {
-1

You should use unique name attribute for each input field. Because the names here is not unique, it works only for first form.

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.