jQuery验证插件未按预期工作。

huangapple go评论74阅读模式
英文:

jQuery validation plugin not working as it should

问题

我正在使用jQuery验证插件进行表单验证,但它不起作用。

当我尝试验证表单时,它只是重新加载页面,什么也不发生。我唯一获得结果的时候是当我的一个输入标签中有"required"时,但我已经有了规则和消息,它们应该正常工作。

我尝试了所有但找不到错误发生在哪里。

另外,我有两个表单,但我只显示第一个表单的代码,因为它们基本相同。我已经检查过我的链接标签,但它们似乎没有错误。

$().ready(function() {
  $("#formFisica").validate({
    onkeyup: false,
    onfocusout: false,
    rules: {
      // 省略其他规则...
    },
    messages: {
      // 省略其他消息...
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js"></script>
<div class="container text-center">
  <!-- 省略其他内容... -->
</div>

<div class="container pt-2" id="containerFisica">
  <form id="formFisica">
    <!-- 省略其他表单元素... -->
    <input type="submit" value="Validate!" />
  </form>
</div>

<div class="container pt-2" id="containerJuridica">
  <form id="formJuridica">
    <!-- 省略其他表单元素... -->
    <input type="submit" value="Validar" />
  </form>
</div>

<script src="./validation.js" type="text/javascript"></script>
<script src="./script.js" type="text/javascript"></script>
英文:

I'm working on a form validation with the jQuery validation plug-in and it's not working properly.

When I try to validate the form it just reloads the page and nothing happens. The only time i get a result is when i have the "required" in one of my input tags, but as I already have the rules and messages, they should be working fine

I tried everything but can't find where I'm doing wrong

Also I have two forms, but I'm showing only the first form's code because they are pretty much the same

I already checked my link tags, but they don't seems to be wrong at all

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$().ready(function() {
  $(&quot;#formFisica&quot;).validate({
    onkeyup: false,
    onfocusout: false,
    rules: {
      nomeFisica: {
        required: true,
      },
      cpfFisica: {
        required: true,
        number: true,
        maxlength: 11
      },
      dddFisica: {
        required: true,
        number: true,
        maxlength: 2
      },
      telFisica: {
        required: true,
        number: true,
        maxlength: 9
      },
      emailFisica: {
        required: true,
        email: true
      },
      confEmailFisica: {
        required: true,
        equalTo: &quot;#emailFisica&quot;
      },
      senhaFisica: {
        required: true,
        minlength: 6,
        maxlength: 15
      },
      confSenhaFisica: {
        required: true,
        equalTo: &quot;#senhaFisica&quot;
      },
    },
    messages: {
      nomeFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;
      },
      cpfFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        number: &quot;Esse campo s&#243; aceita n&#250;meros&quot;,
      },
      dddFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        number: &quot;Esse campo s&#243; aceita n&#250;meros&quot;,
      },
      telFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        number: &quot;Esse campo s&#243; aceita n&#250;meros&quot;,
      },
      emailFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        email: &quot;Digite um endere&#231;o de e-mail valido&quot;
      },
      confEmailFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        equalTo: &quot;Os e-mails n&#227;o correspondem&quot;
      },
      senhaFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;
      },
      confSenhaFisica: {
        required: &quot;Esse campo n&#227;o pode ser vazio&quot;,
        equalTo: &quot;As senhas n&#227;o correspondem&quot;
      },
    }
  });
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js&quot; integrity=&quot;sha512-rstIgDs0xPgmG6RX1Aba4KV5cWJbAMcvRCVmglpam9SoHZiUCyQVDdH2LPlxoHtrv17XWblE/V/PP+Tr04hbtA==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;container text-center&quot;&gt;
&lt;div class=&quot;form-check form-check-inline&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; value=&quot;&quot; id=&quot;flexCheckDefaultFisica&quot;&gt;
&lt;label for=&quot;flexCheckDefaultFisica&quot; class=&quot;form-check-label&quot;&gt;Pessoa F&#237;sica&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check form-check-inline&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;radio&quot; value=&quot;&quot; id=&quot;flexCheckDefaultJuridica&quot;&gt;
&lt;label for=&quot;flexCheckDefaultJuridica&quot; class=&quot;form-check-label&quot;&gt;Pessoa Jur&#237;dica&lt;/label&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;container pt-2&quot; id=&quot;containerFisica&quot;&gt;
&lt;form id=&quot;formFisica&quot;&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;Nome completo&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;nomeFisica&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Insira seu nome&quot; required/&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;CPF&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;cpf&quot; placeholder=&quot;Insira seu CPF&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;DDD&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;dddFisica&quot; placeholder=&quot;DDD&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Telefone&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;telFisica&quot; placeholder=&quot;N&#250;mero de telefone&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;E-mail&lt;/label&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;emailFisica&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Insira seu e-mail&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;Confirmar E-mail&lt;/label&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;confEmailFisica&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Confirme seu e-mail&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Senha&lt;/label&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;senhaFisica&quot; placeholder=&quot;Insira sua senha&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Confirmar Senha&lt;/label&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;confSenhaFisica&quot; placeholder=&quot;Confirme sua senha&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check pt-2&quot;&gt;
&lt;input type=&quot;radio&quot; class=&quot;form-check-input&quot; id=&quot;aceitoEmailFisica&quot; /&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;aceitoEmailFisica&quot;&gt;Aceito receber e-mails&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check pt-2&quot;&gt;
&lt;input type=&quot;radio&quot; class=&quot;form-check-input&quot; value=&quot;&quot; id=&quot;flexCheckChecked&quot; checked/&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckChecked&quot;&gt;Aceito as pol&#237;ticas de privacidade&lt;/label&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; value=&quot;Validate!&quot; /&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div class=&quot;container pt-2&quot; id=&quot;containerJuridica&quot;&gt;
&lt;form id=&quot;formJuridica&quot;&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;Raz&#227;o Social&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;razaoSocial&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Raz&#227;o Social&quot; required&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;CNPJ&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;cnpj&quot; placeholder=&quot;Insira seu CNPJ&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;DDD&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;dddJuridica&quot; placeholder=&quot;DDD&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Telefone&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;telJuridica&quot; placeholder=&quot;N&#250;mero de telefone&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;E-mail&lt;/label&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;emailJuridica&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Insira seu e-mail&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputEmail1&quot;&gt;Confirmar E-mail&lt;/label&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;confEmailJuridica&quot; aria-describedby=&quot;emailHelp&quot; placeholder=&quot;Confirme seu e-mail&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Senha&lt;/label&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;senhaJuridica&quot; placeholder=&quot;Insira sua senha&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group pt-2 pb-2&quot;&gt;
&lt;label for=&quot;exampleInputPassword1&quot;&gt;Confirmar Senha&lt;/label&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;confSenhaJuridica&quot; placeholder=&quot;Confirme sua senha&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check pt-2&quot;&gt;
&lt;input type=&quot;radio&quot; class=&quot;form-check-input&quot; id=&quot;aceitoEmailJuridica&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;aceitoEmailJuridica&quot;&gt;Aceito receber e-mails&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;form-check pt-2&quot;&gt;
&lt;input type=&quot;radio&quot; class=&quot;form-check-input&quot; value=&quot;&quot; id=&quot;flexCheckChecked&quot; checked&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckChecked&quot;&gt;Aceito as pol&#237;ticas de privacidade&lt;/label&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; value=&quot;Validar&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;script src=&quot;./validation.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;./script.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 0

根据文档,插件要求所有输入字段都必须包含name属性。在指定规则和消息时,应使用name属性。

英文:

As per documentation, the plugin requires that all the input fields should contain a name attribute.
When specifying the rules and messages inside .validate() function, you should use the name attribute.

huangapple
  • 本文由 发表于 2023年4月19日 21:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定