简体   繁体   English

如何使用jQuery Validator添加错误消息

[英]How to add error message with jQuery Validator

I got my "contact me" form working with the validation as I want but I can't figure out how to show the error messages. 我可以根据需要使用“与我联系”表单来进行验证,但是我不知道如何显示错误消息。 I've tried to read in the documentation but I just don't understand how to use the errorPlacement function. 我试图阅读文档,但我只是不了解如何使用errorPlacement函数。 Can someone please show me an example on how to do it with the code I got? 有人可以给我一个例子,说明如何使用我得到的代码吗?

<fieldset>
    <input placeholder="Ditt navn" name="contact_name"  id="contact_name" class="contact_input" type="text"  tabindex="1" required autofocus>
</fieldset>
<fieldset>
    <input placeholder="Din email adresse" name="contact_mail" id="contact_mail"  class="contact_input"  type="email" tabindex="2" required>
</fieldset>
<fieldset>
    <input placeholder="Firma navn (valgfri)" name="contact_firmname" id="contact_firmname"  class="contact_input" type="text" tabindex="3">
</fieldset>


$(document).ready(function(){
    $('body').on('keyup','#contact_me',function(){
      $('#contact_me').validate({
        rules: {
          contact_name: {
            required: true,
            remote: location.protocol + '//' + location.host+'/ajax_contact.php'
          },
          contact_mail: {
            required: true,
            remote: location.protocol + '//' + location.host+'/ajax_contact.php'
          },
          contact_firmname: {
          },
        },
        errorPlacement: function(){
            return false;
        },
        submitHandler: function (form) {
          return false;
        }
     });
   });
});

Don't get stucked on documentation if you can't figure out how to do something. 如果您不知道该怎么做,请不要停留在文档上。 Instead look for an example, wich is often 1000 times clearer. 取而代之的是,它的透明度通常高出1000倍。

This article shows a testeable example of what you are trying to reproduce in your code: https://www.webcodegeeks.com/javascript/jquery/jquery-errorplacement-example/ 本文显示了您想要在代码中再现的可验证示例: https ://www.webcodegeeks.com/javascript/jquery/jquery-errorplacement-example/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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