简体   繁体   English

如何通过创建边框红色来突出显示无效的jQuery输入

[英]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检查器查看添加/更改的类;)

You can simply do this by adding CSS styles. 您可以简单地通过添加CSS样式来做到这一点。

If you look the jQuery validation behavior, error class will be added to your input elements. 如果您查看jQuery验证行为,则会将错误类添加到您的输入元素中。

So simple do this 这样简单

.error {
    border: 1px solid #c00;
} 

JSFiddle JSFiddle

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.

相关问题 如何在React中单击按钮突出显示带有红色边框的空强制(*)输入字段? - How to highlight empty mandatory(*) input field with red border click on button in React? 如何将输入蓝色激活边框更改为红色 - How to change input blue activate border to red jQuery(创建输入后突出显示文本) - jQuery (highlight text after creating input) 如果日期在Angular JS中无效,如何使边框变为红色? - how to make border red if date is invalid in angular js? 需要时突出显示带有红色边框的文本框 - Highlight a textbox with red border when it is required 如何在Angular JS中的按钮的输入字段中显示红色边框 - how to show red border in input field on button In angular js 如何在警报Dismis上使输入字段边框变为红色 - How to make input field border red on alert dismis 当文本框字段保持空白时,如何将文本框边框颜色突出显示为红色? - How do I highlight a textbox border color to red when textbox field remain empty? javascript验证失败时如何设置边框或突出显示输入元素? - How to set a border or highlight input element when validation fails with javascript? 如果总计无效,请将字段文本和边框设置为红色 - If total is invalid, set the field text and border to red
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM