Issues with message display validation in jQuery

Validation has been applied to the form, and there are div elements within the input fields with corresponding IDs.

The goal is to display error messages within those divs. Specifically, if an input field is missing, the errorMessage should be set in the respective div before that input field.

An issue arises with the jquery.validate.js library inserting errors between radio elements, which affects the aesthetics of the layout.

Any suggestions on how to address this concern?

For Example (HTML):

 <tr>
    <td>
     <label for='question2'>Some question</label>
</td>
<td valign='top'>
     <input type='radio' name='question2' value='1'/>Answer1<br />
     <input type='radio' name='question2' value='2'/>Answer2<br />
     <input type='radio' name='question2' value='3'/>Answer3<br />
     <input type='radio' name='question2' value='4'/>Answer4<br />
</td>
 </tr>
 <tr>
<td>
     <div id='question23Error'></div>

           //MESSAGE FOR EACH INPUT SHOULD BE INSERTED HERE INDIVIDUALLY

</td>

jQuery:

$(document).ready(function() {
$('#surveyData').validate({

errorContainer: '#errorbox',   //PROBLEM WITH THIS CODE ON FORMS WITH MULTIPLE INPUTS
errorLabelContainer: '#errorbox ul', //
wrapper: 'li', //

    rules: {
        surname : { 
            required:true,
            minlength: 2, 
        }, 
        question1: { 
            required:true,
            minlength: 2, 
        }, 

        question2 : { 
            required: true, 
            }, 

        question3 : {
            required: true, 
        },  
    messages: { 
        surname : {
            required: 'Enter surname!' , 
            minlength: 'Surname should be longer!', 
        }, 
        question1: {
            required: 'Enter your opinion.', 
            minlength: 'Your opinion should be longer', 
        }, 
        question2: { 
            required: 'Please choose radio button.', 
        }, 
        question3: { 
            required: 'Please choose radio button', 
        }, 
    },

    };
});
 });

Answer №1

give this a shot....

issuePosition:function(issue, part)
        {
            if($(part).attr("id")=="problem2")
            {
                $(part).parent().append(issue);
            }else
            {
            $(issue).insertAfter(part);
            }   
        },

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

Enhance the appearance of the table footer by implementing pagination with Bootstrap Angular6 Datatable styling

I utilized the angular 6 datatable to paginate the data in a table format. https://www.npmjs.com/package/angular-6-datatable Everything is functioning properly, but I am struggling with styling the footer of mfBootstrapPaginator. The items appear stack ...

How can you modify two distinct append values individually in AngularJS?

I am facing a challenge with two-way binding. I want to be able to change the name of each appended value from the displayed field options. When I try to append two additional fields, I am unable to change the name of each multiple field from the single fi ...

ReactJs: difficulty in resetting input field to empty string

I have an application using React v 0.13.1 (Old version). I am struggling to update my input field to "" after retrieving the updated value from the database. Scenario: I am updating the input fields by clicking on the button named "Pull&qu ...

Access external variables in next.js outside of its environment configuration

Currently, I am developing my application using next js as the framework. While my environment variables work smoothly within the context of next js, I am facing a challenge when it comes to utilizing them outside of this scope. An option is to use dotenv ...

Double your audio experience with Webaudio by playing the sound twice

While working on WebAudio experimentation, I encountered an issue with loading a sound using the JavaScript code below. function playAudio(){ var audio = document.getElementById('music'); var audioContext = new webkitAudioContext(); ...

Adding days to a HijriDate: A step-by-step guide

I need help with two text boxes. The first box is for selecting the StartDate from an Islamic calendar, which I am currently using jQuery Calendars Datepicker for. The second textbox, ExpireDate, should automatically be set to 60 days after the selected St ...

How can one include a URL as a "URL parameter" when using Express?

In my Node.js application, I've set up a router to listen for requests at api/shorten/: router.get('api/shorten/:longUrl', function(req, res, next) { console.log(req.params.longUrl); } When I enter something like: http://l ...

Angular Material (8) error code S2591: The variable 'require' is not defined in the current scope

Currently, I am attempting to record the date and time in the JavaScript console. Despite the code successfully logging the dates, an error message persists: Note: The code is functioning properly, with the dates being displayed in the console. It is only ...

Having trouble with the mouse trail code on codepen.io

I am currently attempting to integrate this specific CodePen sample into a Hugo template called Somrat Theme. I'm facing challenges in deciding where to place the HTML file; only the 'no cursor' section should go into style.css, and I need ...

Unspecified data returned from PHP script via jQuery AJAX

Encountering an issue while trying to use AJAX to get a PHP response from a form. The JavaScript appears to be correct as it functions when the content of login_ajax.php is reduced to just: echo 'CORRECT' //or echo 'INCORRECT' Howev ...

How can I make the columns in Next.js / Tailwind expand horizontally first instead of vertically?

Recently, I decided to customize the Next.js Image Gallery starter for some hands-on experience in full stack development with Next.js. My goal was to create a chronological photo gallery of a recent trip, but encountered an issue where the responsive colu ...

unique array that shuffles every time it is reloaded, utilizing only javascript

I have created a string array that holds 6 different classes. Whenever I click a button, a new class is generated randomly. However, the issue arises when I click the button again and get the same class instead of a new random one. Even after reloading the ...

Can you choose an option from a dropdown menu without relying on jQuery?

Can a dropdown list item be selected using AngularJS instead of jQuery? I have successfully accomplished this using jQuery, but I am curious if it can be done with AngularJS. This is how I achieved it using jQuery: var dropdownlist = $("select").data("k ...

CORS blocked in the live environment

error + whole page As a newcomer to JavaScript, I recently deployed my project. During development, everything was functioning well. However, I am now facing an issue with CORS when attempting to sign in (registration works without any problems, confirmin ...

What's causing this error with jQuery and JSON?

When utilizing the following code snippet: - $.getJSON("admin.php?format=json", { module: "data", action: "allBusinessUnitsByClientName", clientname : $('#client').val() }, function(json) { $.each(json.items, function(i,item){ alert ...

Disable or eliminate the event listener

Working on my Angular2 application, I've set up an RxJS timer that sends notifications to users when they are logged in. The twist is, the notification should only be sent if the tab is active; otherwise, the scheduler should pause or stop. I have man ...

Is it possible to retrieve the complete file path in a form?

My goal is to retrieve a file using an input element of type "file". This element is located within a partial view, and I need to send it to the controller or request it there using "Request.Form["inputFile"];". However, this method only provides me with t ...

Arranging th tags level and at equal height within a datable using HTML and CSS

I am looking to arrange my top headers in a table so that the red square icon is always next to the first span without using absolute positioning. Is there a way to achieve this without using absolute position? table.dataTable thead span.sort-icon { ...

What is the best way to dynamically populate a table with JSON data that is received from an API response?

Currently, I have a table that contains the FORM element: <table id="example" class="sortable"> <caption><h3><strong>Product inventory list</strong></h3></caption> <thead> <tr ...

Exploring ways to retrieve a video thumbnail with VueJS3

I am currently working on a project to create a simple program that retrieves movies from my AWS-S3 bucket and transforms them into picture thumbnails. Below is the code I have written: <template> <img :src="imgURL" class="card- ...