The validation error is displayed within the textbox instead of beside the label

Hey there! I encountered a rather peculiar issue while working on my jQuery mobile webapp. I decided to implement jqueryvalidation for form validation, which is pretty neat, except for one hiccup - the error messages are showing up inside the input fields instead of below them. Any ideas on how to fix this? Check out my jsfiddle here: http://jsfiddle.net/chJ8B/3/

$().ready(function() {      
        // validate new customer form
        $("#newCustomer").validate({

            errorPlacement: function(error, element) {
            error.insertAfter(element); // <- the default
        },

            rules: {
                surname: "required",
                phoneNumber: "required"


            },
            messages: {
                surname: "Please enter the surname/company name",
            }
    });
     });

<div class="ui-field-contain"><label for="surname">Company/Surname</label><input type="text" name="surname" id="surname"  ></div>
<div class="ui-field-contain"><label for="forename">Forename</label><input type="text" name="forename" id="forename" value=""></div>

Answer №1

If you're encountering issues with jQuery mobile markup, try placing the error element after the parent of the current element.

$("#newCustomer").validate({
    errorPlacement: function(error, element) {
        error.insertAfter(element.parent()); // <- this is the default
    },
    rules: {
        surname: "required",
        phoneNumber: "required"
    },
    messages: {
        surname: "Please enter the surname/company name",
    }
});

Check out the demo here: Fiddle


In some cases, you may need to incorporate conditional placements depending on the specific type of markup you are working with.

Answer №2

Modify the error positioning using the following code snippet:

errorPlacement: function (error, element) {
  error.insertAfter(element.parent()); // <- this is the current setting
}

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

Display hyperlinks upon hovering over text

In the sign-up form, there are options for two different types of users: teachers and students. When the sign-up option is selected, a drop-down menu with choices for teacher and student will appear. However, I would like it to function more like other w ...

Modify the current style of the MUI table

I'm working on a table with sorting functionality similar to the mui website's "sorting and selecting" feature: https://material-ui.com/components/tables/ My goal is to modify the color of the header text that is selected for sorting. I attempte ...

JavaScript threw an error because it encountered an unexpected or invalid token

I can't seem to identify the error in this code. Can someone please help me correct it? document.write(' <div> <a href=http://www.socialsignal.ir style=\'\\padding:5px;text-decoration: none;border: 1px solid #555;ba ...

Observing an element within AngularJS

Struggling to watch an object in AngularJs 2. In a directive, I have an object like this: scope.timelineFilter = {hasCapacity: true, onlyActivated: true, companyIdList: []}; I use 2 JQuery functions to change values of the object (adding a third soon) ...

What is the solution for fixing scrolling while keeping the header fixed and ensuring that the widths of headers and cells are equal?

Is there a way to set a minimum width for headers in a table and provide a scroll option if the total width exceeds 100% of the table width? Additionally, how can we ensure that the width of the header and td elements are equal? Below is the HTML code: ht ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...

Align the dropdown menu in the center of every navigation item

A div of exact dimensions, measuring 800px wide and 50px tall, contains an unordered list of 6 elements that are centered within the div. The alignment is currently working as intended. My next goal is to create a dropdown list from each element. However, ...

When I double click on the navigation bar, it automatically selects the div behind it, even if there is no visible text. Is there

My slider has navigation arrows, and when the arrow is double-clicked, a div behind the #slider1, which is .content-wrapper, gets selected unexpectedly. Here is a screenshot showing the issue: http://gyazo.com/e66ab776b60455d8b496b46e3077099a If the arrow ...

Determine the total cost based on the quantity purchased

I created a webpage for employees to select an item from a dropdown menu, and it will automatically display the price of that item. Check out my code below: <script> $(document).ready(function() { $('#price_input').on('change' ...

Error retrieving property in Internet Explorer when using AJAX

Encountering error message Unable to get property ‘replace’ of undefined or null reference on line var ajax_html = $(xml).find("#search-ajax-content").html(); while utilizing AJAX in IE (specifically testing with IE11). This code operates without any g ...

Display a message indicating unavailability when no events are scheduled and adjust the background color in FullCalendar

In one of my projects, I am utilizing jQuery FullCalendar to schedule appointments for doctors. The doctors should be able to specify their availability between 9 AM - 5 PM on weekdays. If this is not set, the background color of the spans of time not boo ...

Place the first paragraph within a div above another paragraph in the same div

Presently, I have this item: https://i.stack.imgur.com/0dBd9.png however, my objective is to achieve this look: https://i.stack.imgur.com/NEBZf.png The code snippet in question is as follows: .bonus-div { width: 290px; height: 29px; ma ...

Make sure to add CSS styles to the "ul" class first, followed by

I'm looking to update the color of the links in my footer using CSS. I want to change the color of all a items that have the same classes, but I'm unsure of how to do this with CSS. Here's the code snippet: <div class="col-lg-3 ...

Tips for personalizing PNG images within an SVG element

Looking to update the color of a PNG image within an SVG tag. The PNG images I have are transparent, and I want to customize their colors based on user selections. How can this be achieved? Is there anyone out there who can assist me? <HoodieSvg ...

Incorporating a scrolling background image behind my other <div> columns

I've been working on a website for a relative and have organized the page into columns wrapped in div elements, However, I'm struggling to get the columns centered over the scrolling background image, If I place the background div element at th ...

Incorporate a section of text to flow around a floated image within a paragraph

https://i.sstatic.net/s5aFq.png How can I achieve the text wrapping above an image similar to the layout in the sunflower picture? I have tried using relative positioning and adjusting the top property, but it doesn't allow the text to wrap around th ...

Checking if the section id matches the href within an anchor tag using a jQuery if statement

Purpose: Ensure that the yes class remains on an element unless it is being added to another. The code snippet below demonstrates this: jQuery(function($){ $(window).scroll(function() { var position = $(this).scrollTop(); if ($(&apos ...

Showing a message only when there is input in the search bar using jQuery

I'm currently working with a Google Instant style search script that is written in jQuery and queries a PHP file. The issue I am facing is that even when the search box is empty and there are no search terms, the script still displays a message saying ...

An unusual CSS phenomenon with z-index

I've encountered a peculiar issue with my z-indexing. I have an image positioned relatively with a z-index of 1, and it should be above my navigation that is positioned absolutely with a z-index of -1. The problem is that upon page load, the image ap ...

Ways to stop click function execution in jQuery

Here is the HTML code I am working with: <h4 class="milestonehead" style="cursor: pointer;" onclick="editFun('35');"> Some Header <img src="/images/delete.gif" style="float: right;" onclick="deletefun('35');"> < ...