jQuery验证errorLabelContainer

时间:2017-05-10 07:12:40

标签: javascript jquery html formvalidation-plugin

请帮我验证,我一直在尝试自定义错误信息应该显示在哪里,但我不能和我一直在尝试搜索网络,但似乎我不明白" errorLabelContainer&#34 ;或validate.js

我有两个输入,名称和电子邮件。我想控制消息显示的位置而不是输入标记



<script>
jQuery(function($) {
$('#form').validate({
	errorClass: "errorOne",
	rules: {
		firstname: {
			required: true
		},
		email: {
			required: true
		}
	},
	messages: {
		firstname: {
			required: 'Please enter name',
			errorElement : 'div',
			errorLabelContainer: 'errorOne'
		},
		email: {
			required: 'Please enter a valid email address',
			errorElement : 'div',
			errorLabelContainer: '.errorTwo'
		}
	}
});
</script>
&#13;
<style>
	.errorOne{
		background: lightgreen;
		min-height: 20px;
}
	.errorTwo{
		background: pink;
		min-height: 20px;
}
	</style>
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery validation plug-in - main demo</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>

</head>
<body>


<form id="form" method="post" action="">
	<div style="border:1px solid red; height: 500px">
		<div class="col-md-8">
			<label for="firstname">Enter Name:</label>
			<input class="form-control"  type="text" name="firstname" />
			<label for="email">Enter Email:</label>
			<input class="form-control" type="text" name="email" /><br>
			<input type="submit" class="button" value="Submit" />
		</div>
		<div class="col-md-4">
			<ul>
				<li>
					<p>The name error must be displayed in the div with class errorOne</p>
					<div class="errorOne"></div>
				</li>
				<li>
					<p>The email error must be displayed in the div with class errorTwo</p>
					<div class="errorTwo"></div>
				</li>
			</ul>
		</div>
	</div>

</form>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用errorClass选项更改错误类,如下所示:

$('#form').validate({
    errorClass: "errorOne", // Now class of '.errorOne' will be used for errors 
    rules: {
      firstname: {
        required: true
      },
      email: {
        required: true
      }
    },
    messages: {
        firstname: {
            errorElement : 'div',
        },
        email: {
            errorElement : 'div',
        }
    },

  });
});

请注意,errorOne前面没有点。

<强>更新

如果你想为每个输入添加不同的错误类,那么在不调整jQuery Validator的情况下就不可能这样做。

实现这一目标的一种更简单的方法是为输入errorElement定义一个CSS类(默认情况下为label)。

像这样:

input[name='firstname']+label{ color: deeppink; }

input[name='email']+label{ color:pink; }

看看这个Demo