jquery - 如何为 jquery 验证错误消息设置动画并突出显示该字段?
问题描述
我有一个使用 jquery 验证的登录页面,并且正在使用插件 animate.style 进行 css 动画。
我试图让表单字段输入框突出显示,错误消息淡入并出现在文本字段下方。但是,我拼凑的代码非常有问题,我相信可以改进。
由于输入框是包含图标的合并字段,因此我不确定我是否定位正确。
我的表单代码如下所示:
<form class="js-validation-signin" action="login_check.php" method="post" name="loginform" id="loginform">
<div class="form-group">
<label class="text-label" for="username">Username:</label>
<div class="input-group input-group-merge">
<input name="username" id="username" type="text" class="form-control form-control-prepended"
placeholder="Enter your username">
<div class="input-group-prepend">
<div class="input-group-text">
<span class="fa fa-user-alt"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="text-label" for="password_2">Password:</label>
<div class="input-group input-group-merge">
<input name="password" id="password" type="password" class="form-control form-control-prepended"
placeholder="Enter your password">
<div class="input-group-prepend">
<div class="input-group-text">
<span class="fa fa-key"></span>
</div>
</div>
</div>
</div>
<div class="form-group">
<input type="submit" value="Sign In" class="btn btn-primary btn-block" name="submit">
</div>
我使用的验证码是:
$(function() {
// Initialize form validation on the registration form.
// It has the name attribute "registration"
$("form[name='loginform']").validate({
// Specify validation rules
errorClass: "invalid-feedback is-invalid",
validClass: "is-valid",
errorElement: "div",
errorPlacement: function(error, element) {
//error.appendTo( element.closest(".form-group"));
jQuery(element).parents(".form-group > div").append(error)
},
highlight: function(element) {
$(element).fadeOut(function() {
$(element).fadeIn();
$(element).addClass("is-invalid");
});
},
rules: {
// The key name on the left side is the name attribute
// of an input field. Validation rules are defined
// on the right side
username: "required",
password: "required"
},
// Specify validation error messages
messages: {
username: "Please enter your username",
password: "Please enter your password",
url: "Please enter a url"
},
// Make sure the form is submitted to the destination defined
// in the "action" attribute of the form when valid
submitHandler: function(form) {
form.submit();
}
});
});
非常感谢帮助!
解决方案
推荐阅读
- python - 从具有固定背景的图像中删除背景
- angular - 如何测试一个动作是从 NGXS 的另一个动作中分派的?
- python - Opencv Contour 无法捕获减号和等号
- python - 显示前 2 名差异最大的汽车记录
- javascript - Javascript映射数组的每个第n个元素
- python - 搜索python字典的有效方法
- asp.net - 如何使用 Mailchimp.Net.V3 将 mailchimp 与 asp.net_core 项目集成?
- error-handling - 如何防止 Apollo 客户端 useQuery 中的错误传播?
- react-native - 我有时会意外结束流
- java - 如何从 Android 应用程序调用带有不记名令牌的 REST API?