在jquery-form-validator插件中放置错误

时间:2015-05-20 11:10:24

标签: jquery plugins jquery-form-validator

<html lang="en" dir="ltr"><head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>jQuery Form Validator</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
</head>
<form action="" id="registration-form">
<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>E-mail</td>
    <td><input id="email" name="email" data-validation="email"></td>
  </tr>
  <tr>
    <td></td>
    <td><label for="email" generated="true" class="error"></label></td>
  </tr>
  <tr>
    <td>User name</td>
   <td> <input id="user" name="user" data-validation="length alphanumeric" 
         data-validation-length="3-12" 
         data-validation-error-msg="User name has to be an alphanumeric value (3-12 chars)"></td>
  </tr>
  <tr>
    <td></td>
    <td><label for="user" generated="true" class="error"></label></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><input id="pass_confirmation" name="pass_confirmation" data-validation="strength" 
         data-validation-strength="2"></td>
  </tr>
  <tr>
    <td></td>
    <td><label for="pass_confirmation"></label></td>
  </tr>
  <tr>
    <td>Repeat password</td>
    <td><input name="pass" data-validation="confirmation"></td>
  </tr>
  <tr>
    <td>Birth date</td>
    <td><input name="birth" data-validation="birthdate" 
         data-validation-help="yyyy-mm-dd"></td>
  </tr>
  <tr>
    <td>Country</td>
    <td><input name="country" id="country" data-validation="country"></td>
  </tr>
  <tr>
    <td>Profile image</td>
     <td><input name="image" type="file" data-validation="mime size required" 
         data-validation-allowing="jpg, png" 
         data-validation-max-size="300kb" 
         data-validation-error-msg-required="No image selected"></td>
  </tr>
  <tr>
    <td>User Presentation (<span id="pres-max-length">100</span> characters left)</td>
    <td><textarea name="presentation" id="presentation"></textarea></td>
  </tr>
  <tr>
    <td colspan="2"><input type="checkbox" data-validation="required" 
         data-validation-error-msg="You have to agree to our terms">
    I agree to the <a href="..." target="_blank">terms of service</a></td>
  </tr>
  <tr>
    <td><input type="submit" value="Validate"></td>
    <td><input type="reset" value="Reset form"></td>
  </tr>
</table>  
</form>
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.2.1/jquery.form-validator.min.js"></script>
<script>

  $.validate({
    modules : 'location, date, security, file',
    onModulesLoaded : function() {
      //$('#country').suggestCountry();

    }

  });

  // Restrict presentation length
  $('#presentation').restrictLength( $('#pres-max-length') );

</script>
</html>

我正在研究表单验证,经过一段时间我找到了jquery-form-validator插件,这将帮助我验证。但现在我试图找出如何改变错误位置。现在所有错误都显示在字段名称前面。我想在此

中显示电子邮件的错误

<label for="user" generated="true" class="error"></label>

并且还有任何形式验证的PHP

2 个答案:

答案 0 :(得分:0)

使用属性data-validation-error-msg-container指出某个输入的错误消息的位置

<input data-validation="..." 
       data-validation-error-msg-container="#some-where-else" />
<div id="some-where-else"></div>

在谈到这一点时,你还有其他一些选择。您可以在http://formvalidator.net/#configuration_position

上阅读相关内容

答案 1 :(得分:-1)

使用可以在此处参考他们的文档http://formvalidation.io/examples/showing-message-custom-area/以获取自定义错误消息区域

或者您可以使用此代码:

err: {
        container: '#messages'
    },

您可以在每个字段的jQuery验证插件代码中尝试此代码。