如何选择使用jquery validate时显示的错误消息?
这是我的初学者:
$('#msform').validate({
//errorPlacement: function() {},
messages: {
email: {
email: "Please enter a valid email.",
}
},
highlight: function(element) {
$(element).parent().addClass('has-error');
$(element).css('border-color', '#a94442');
},
unhighlight: function(element) {
$(element).parent().removeClass('has-error');
$(element).css('border-color', '');
}
});
我不希望显示required
标签错误。我只想显示无效的电子邮件标签。这可能吗?
答案 0 :(得分:-1)
Nic,请尝试以下代码:
<html>
<head>
<meta charset="utf-8">
<title>Makes "field" always required. Nothing and blanks are invalid.</title>
<link rel="stylesheet" href="https://jqueryvalidation.org/files/demo/site-demos.css">
</head>
<body>
<form id="msform">
<label for="field">Required: </label>
<input type="text" class="left" id="email" name="email">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#msform").validate({
rules: {
email: "required email",
},
messages: {
email: {
required: "",
email: "Invalid Email",
}
}
});
</script>
</body>
</html>