[英]how to highlight invalid jquery input by creating border red
How can i highlight error in input jquery validation?? 如何在输入jquery验证中突出显示错误?
please help me 请帮我
$(function () {
// Setup form validation on the #register-form element
$("#register-form").validate({
// Specify the validation rules
rules: {
firstname: "required",
lastname: "required",
month: "required",
day: "required",
year: "required",
gender: "required",
email: "required",
phone: {
required: true,
minlength: 11
},
password: {
required: true,
minlength: 6,
equalTo: "#confirm-password"
},
submitHandler: function (form) {
form.submit();
}
});
});
this two element put in jquery validate to highlight error 放入jQuery的这两个元素验证突出显示错误
errorElement: "div",
errorClass: 'help-block animation-slideDown',
errorPlacement: function (error, element) {
error.insertAfter(element);
element.addClass("edited");
element.parents(".form-control-focus > div").append(error);
error.insertAfter(element);
element.parents(".form-group > div").append(error);
},
highlight: function (e)
{
$(e).closest(".input-group ").parent().removeClass("has-error").addClass("has-error"), $(e).closest(".help-block").remove();
$(e).closest(".form-group").removeClass("has-error").addClass("has-error"), $(e).closest(".help-block").remove();
},
You have to edit CSS - not Javascript. 您必须编辑CSS-而不是Javascript。 Input field with error will get "error" class attached to itself.
输入错误的字段将附加“错误”类。
Next time - use HTML inspector to see what classes are added/changed ;) 下次-使用HTML检查器查看添加/更改的类;)
The documentation of the jQuery validate plugin explains how to do it: jQuery validate插件的文档说明了如何执行此操作:
errorClass (default:
"error"
)errorClass (默认:
"error"
)Type: String
类型:字符串
Use this class to create error labels, to look for existing error labels and to add it to invalid elements.
使用此类创建错误标签,查找现有的错误标签并将其添加到无效元素。
Example: Sets the error class to “invalid”.
示例:将错误类别设置为“无效”。
$(".selector").validate({ errorClass: "invalid" });
Also the valid one: 也是有效的:
$(".selector").validate({
validClass: "success"
});
You will of course need to add these two classes to your css. 您当然需要将这两个类添加到CSS中。
.invalid { border: 1px solid red }
还有一些需要考虑的是HTML 5的本机浏览器功能。您还可以根据输入类型使用必填属性和其他验证属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.