Is it frowned upon to modify Jquery Validation for the purpose of achieving a customized style?

When it comes to adding an icon after an input field when a required field is not filled up, my friend and I have different approaches.

My idea is to modify the highlight function in Jquery.Validation by adding a class to the parent element of the input (which happens to be a div) so that I can style it using a pseudo element.

On the other hand, my friend suggests adding a div after the input for styling purposes. He did mention, however, that using HTML elements solely for styling might not be the best practice.

Despite this, he also believes that making modifications to a library is not a good practice either. So, we are left wondering which approach would be more suitable.

Answer №1

Modifying a library is not recommended practice.

However, there are two effective approaches that can be taken. The jQuery validation plugin offers various options to customize behavior, indicating its flexibility and intended functionality.

To apply CSS styling options, define the valid and error classes in your stylesheet:

form.error {
    color: red;
    border: 1px solid red; // for illustration purposes only
}

form.valid {
    color: green;
}

If additional logic such as adding or moving elements is necessary, implement custom actions within the errorPlacement, highlight, and unhighlight functions:

$("form").validate({
    rules: {},
    messages: {},
    highlight: function(element, errorClass, validClass) {
        $(element).addClass(errorClass); 
        // add custom actions here
    }, 
    unhighlight: function(element, errorClass, validClass) {
        $(element).removeClass(errorClass); 
        // add custom actions here
    }, 
    errorPlacement: function(error, element) {
        var alert = $("<div/>").addClass("alert alert-danger").insertAfter(element); 
        alert.append(error);
    }
});

As stated in the documentation:

errorPlacement determines where error labels are placed.
highlight defines how invalid fields are highlighted.
unhighlight specifies how to revert highlight actions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the HTML Canvas to cover the entire screen

I am trying to adjust the HTML Canvas element so that its width matches the width of the browser window and its height matches the height of the browser window. Below is the code snippet I am using: HTML: <body> <canvas id="gameCanvas" ...

Columns with adjustable widths

I am looking to create a layout with 3 columns (which may change, could be up to 4) that are all flexible width and fill the screen. Does anyone know if this is possible using CSS? Basically, I want three blocks lined up horizontally that take up the enti ...

The properties of the box model applied to unidentified inline boxes

Reference: CSS Specification Text directly contained inside a block container element (not within an inline element) is considered as an anonymous inline element. For example, in an HTML document like this: <p>Some <em>emphasized</em> ...

Open Chrome in fullscreen mode directly from your Android home screen without the statusbar displayed

One of my clients is looking to have a specific website launched from an icon on an Android tablet. Since he is leasing the tablets, we have full control over the hardware. The goal is for these tablets to exclusively display his site (which is a slideshow ...

Quickest method to create a new empty stylesheet in the developer tool of Chrome's inspector ("Inspector stylesheet")

When testing CSS modifications live in Chrome, I typically use the following method: Right click > Inspect Then, I use the bottom right panel and click the + button to add a new class which I can edit directly. https://i.sstatic.net/VX57R.jpg Howe ...

Making a standard AJAX request using jQuery without including the XHR header

I am looking to make an ajax-type request using the following headers (or something similar): GET example.com/ajaxapi/article/1 HTTP/1.1 Host: localhost Accept: application/hal+json Cache-Control: no-cache The key aspect here is to accomplish this withou ...

The margin in the DIV is not aligning correctly with the specified requirements

I encountered a puzzling issue with a small problem, and I am certain that I must be making a mistake somewhere. I experimented with two divs styled using different CSS. <div id="main"> <div id="internal"> hello </div> < ...

Monitoring the modifications of a linked component in React: A guide

I am facing an issue where I need to store the text of a referenced element in an array. My goal is to first display the text of each paragraph element with the "ebookName" class in the console before storing it in the array. However, I have encountered a ...

Using jQuery to retrieve the text content of child elements

Struggling to extract the text of child elements using jQuery. I've been at this for a couple of days and can't seem to make it work. If anyone could spare a moment to review, I would greatly appreciate it! SCRIPT: function generateRemoveSect ...

Is it possible to use Prettier in ESLint for formatting Markdown and CSS files?

At the moment, I have Prettier set up with ESLint using the configuration provided in my .eslintrc.json file: { "extends": ["react-app", "plugin:prettier/recommended"] } Everything works smoothly for linting and formatting JavaScript files. However, I ...

I'm looking to extract information from a webpage using Python by inputting specific criteria

Looking to gather information from a specific website - Link and save it either in a database or another location. The process involves providing input parameters such as entering the vehicle number, submitting it, then verifying if it matches your car. N ...

Tips for initializing a jstree with no content?

When I click a button, I send a key to the controller and retrieve my lists using JSON. The array inside my lists serves as my children in my jstree. $("#btnSearch").on("click", function () { alert("I'm also here"); $.ajax({ ...

Rails New Action fails to render HTML during AJAX GET request

Currently, I am in the midst of a project where I made an upgrade to the rails gem from version 2.3.5 to version 2.3.11. However, now I am encountering an issue with getting the controller actions to return HTML via jQuery ajax get requests. An example sce ...

Incorporating a View into a Sidebar in CakePHP

I'm currently facing some challenges understanding the concepts of CakePHP's views, blocks, and layouts. My goal is to have a left and right sidebar displayed on every page, with content that may vary. Right now, I have the right sidebar set up ...

What is the process for altering the active color of a materialize icon?

Currently working on a web application for my school project and incorporating the Materialize framework. However, I'm having trouble modifying the default teal color of the active icon. https://i.sstatic.net/9eEfM.jpg ...

Is it possible to create this layout using Bootstrap 5? I want to stack spans, with one inside the container and one outside

Trying to achieve a specific design using Bootstrap 5.3. https://i.sstatic.net/STifm.jpg This design should be placed over the showcase, with the following code for the showcase displayed below: <div class="showcase d-flex flex-column"&g ...

How can I make the hover effect only apply to the text and not the entire box?

I'm wondering how I can make the :hover property only affect individual letters in my navigation bar, instead of hovering over the entire box. * { margin: 0px; padding: 0px; } .navigation-bar { height: 3.2em; background: gray; text-align: ...

Issue with the width on Mobile Safari

Encountering a strange problem with mobile Safari on iPads and iPhones where the body and some divs are getting cut off in the middle. Despite trying various solutions, I can't seem to fix it. The website works perfectly fine on desktop but not on mob ...

Using checkboxes to select all in AngularJS

Recently, I started working with Angularjs and came across some legacy code. I am trying to figure out a way to select all checkboxes once the parent checkbox is selected. Here is the parent checkbox: <div class="form-group"> <label for="test ...

Creating CSS wrappers around div elements of varying sizes

I am looking for a way to "wrap" different divs of varying sizes, similar to how Microsoft Word wraps text around an image. I have a simplified structure outlined below: <div id = "div1"></div> <div id = "div2"></div> <div id = ...