jquery.validate.js does not work without form-group (Bootstrap)

Asked

Viewed 465 times

1

The validation of jquery.validate.js only works when the input this one inside
<div class="form-group">, for example if I put the code:

  <div class="row">
     <div class="col-sm-12 col-md-12 col-lg-12">
         <div class="inner-addon left-addon">
             <i class="glyphicon glyphicon-log-in"></i>
             <input id="id_codigo_empresa" name="codigo_empresa" 
                  type="number" class="form-control" maxlength="6"
                          placeholder="Código da entidade" required>
         </div>
     </div>
  </div>

If you leave the input empty and click the button Submit it does not show the message 'Campo requerido' on screen, but if I do:

 <div class="form-group">
    <div class="row">
         <div class="col-sm-12 col-md-12 col-lg-12">
             <div class="inner-addon left-addon">
                 <i class="glyphicon glyphicon-log-in"></i>
                 <input id="id_codigo_empresa" name="codigo_empresa"
                      type="number" class="form-control" maxlength="6" 
                              placeholder="Código da entidade" required>
             </div>
         </div>
    </div>
 </div>

Adding a div with form-group works, but I don’t want to use the form-group because it takes formatting from the field and then changes the design.

Is there any way to use the validator without the form-group?

Just to complement, it validates, but does not display the message without the form-group.

If I add my style modifications to the form group, it gets kind of bizarre. below field image with the form-group and without it, see that the input has become square and no edges and as had decreased the margin-Botton the error label is behind the input.

inserir a descrição da imagem aqui

  • You can provide a Fiddle example using these bootstrap classes with Validator?

  • You said: "Just to complement, it validates, but does not display the message without the input-group.", Have you checked what this class does in the fields? Give a check on arquivo.css, where there’s this class, try inspect the page elements (F12), to see how the page is being mounted.

  • 1

    instead of you trying to take the div form group, why not try to give a ! Important in your own css style? this way the form will not be disfigured as you said

  • @Brunocastro , I tried but it’s kind of bizarre, I posted an image

2 answers

0


The .validate plugin should be used in ID of the form, as per Example fiddle.


Here is the Snippet used, however it is with error in the script, but it is the same code of Example fiddle.

$("#formulario").validate();
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row">
  <div class="col-sm-12 col-md-12 col-lg-12">
    <div class="inner-addon left-addon">
      <i class="glyphicon glyphicon-log-in"></i>
      <form class="cmxform" id="formulario" method="get" action="">
        <fieldset>
          <legend>Por favor entre com seu nome, email e um comentário.</legend>
          <p>
            <label for="cname">Nome (Requer, no mínimo 2 caracteres)</label>
            <input id="cname" name="name" minlength="2" type="text" required>
          </p>
          <p>
            <input id="id_codigo_empresa" name="codigo_empresa"
               type="number" class="form-control" maxlength="6"
               placeholder="Código da entidade" required>
          </p>
          <p>
            <label for="cemail">E-Mail (requerido)</label>
            <input id="cemail" type="email" name="email" required>
          </p>
          <p>
            <label for="curl">URL (opcional)</label>
            <input id="curl" type="url" name="url">
          </p>
          <p>
            <label for="ccomment">Seu comentário (requerido)</label>
            <textarea id="ccomment" name="comment" required></textarea>
          </p>
          <p>
            <input class="submit" type="submit" value="Submit">
          </p>
        </fieldset>
      </form>
    </div>
  </div>
</div>


I used the jquery.validate.js of the site jqueryvalidation.org, I don’t know if it’s the same one you used, but in Fiddle is working perfectly.

  • worked his code, but I came across another problem, I’m using the form-Wizard of bootstrap ( <fieldset>), the one that has tabs and it already has a 'next' and previous button, so I only give a Ubmit in the last step. you think I should stop using this form-Wizard?

  • Him (Form-Wizard) is giving any problem? If not, I see no problem of continuing using.

  • If the answer helped, do not forget to mark as correct and evaluate ;)

  • 1

    I’ve already marked it as correct and confirmed the 50 points, which helped me, thank you. I’ll just see how I can make the next button call this event, I actually see that it validates, but the message does not appear.

  • Consider create another question to address this specific problem ;)

-1

Simple, the jquery.validade positions the elements using position:absolute therefore for a coherent functioning it will be necessary that it is enclosed in an element with position:relative.

To resolve it would suffice that the div.inner-addon is with the following attribute:

.inner-addon {
    position: relative
}
  • I did as you showed, I added this style to . Inner-addon but it didn’t work, I tried other Divs, but nothing, I made an edit on my question, the correct is form-group and not input-group.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.