When using JQuery Validation on a small screen, error messages can cause the button to shift downwards

After successfully creating an email form with jquery validation error style that appears when there's an error, I encountered a problem. When the screen width is reduced or viewed on a small screen, the error message pushes down the button.

I've attempted using position: absolute and display:inline-block for the error style, but to no avail. I'm currently stuck and in need of assistance from a seasoned CSS expert.

So, how can I prevent the button from being pushed down when an error message appears on a small screen or during browser window resizing?

Here you can find the code and fiddle: http://jsfiddle.net/psbq8vkj/4/

CSS:

.error-class {
    color:red; z-index:1; font-size:15px; 
}

.guestlist-form.error-class {
    color:red; z-index:1;
}

.guestlistfield.error-class {
    position:relative; display:inline-block;
}

.guestlist-form .button {
    position:relative; z-index:2;
}

input.error-class { border:3px solid red;}

input.error-class:focus { border:3px solid #f90;}

.valid-class {
    color:black;
}

.emailaddress:focus, textarea:focus {
    border:3px solid #f90;
}

@media (min-width:765px) { 
/* Styles for wider screens */
}

@media (max-width:764px) { 
/* Styles for smaller screens */
}

/* Additional styles */

                .guestlistfield
            {
                /* Styling for guest list field */
            }

        /* More styles */

.form input.guestlistfield:enabled,
            form select:enabled,
            form textarea:enabled
{

    background:#fff;

}
            
                                
/* Button Style */
.button
{
    /* Styling for buttons */
}

    .button:focus
{
    /* Focus styling for buttons */
}

.button:hover
{
    /* Hover effect for buttons */
}

#updates 
{ 
/* Styles for updates section */
}



    @media (max-width:280px) { 
.signuptitle {font-size:15px; color:#fff; margin-top:50px;}
}


/* Responsive design based on screen size */

HTML:

    
<!-- HTML markup -->

JQuery:


// JQuery validation script
$(".guestlist-form").validate({
    errorClass: "error-class",
    validClass: "valid-class",  
    rules: {
        // Validation rules
    },
    messages: {
        // Custom error messages
    }
});

Answer №1

the error code will be shown after the initial input field, disrupting your html and css layout:

insert

<div class="myerror" style="margin-top:-20px;"></div>
right after <div class="guestlist">

within your jquery .validate function include

errorPlacement: function(error, element) {
  $(".myerror").html(error)
},

this will now display the error message beneath the input fields.

responding to the comment:

if you want to customize the error message, utilize the success function as follows:

success: function(label) {
    label.addClass("valid-class").text("Ok!")
}

Your updated fiddle here

for more information visit the official site here

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

Generating a JSON structure by iterating through list items

I'm struggling to create a JSON object by looping through my list items and need some guidance. My end goal is to have a PHP array like this: $positions = array( 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 1); To achieve this, I m ...

"Incorporate Bootstrap drop-down and button together for a stylish group feature

I am looking to format the code below to resemble the layout shown in the image using Bootstrap 3.3. When there isn't enough space to show all elements in a single line, I want them to stack vertically so that each element utilizes the entire width of ...

Using jQuery .each() within a PHP foreach loop: A guide

In my code, I have a foreach loop that iterates through an array of images, along with some JavaScript logic to add different classes based on whether the width is greater than or less than the height. The issue I'm facing is that the if function onl ...

Trigger a keypress event following the activation of a button

I've been trying to simulate the press of the backspace key on the keyboard after clicking a button, but all my attempts have been unsuccessful. Here is the code I'm using: function rtf(){ document.getElementById("u0u").focus(); $('#u0u ...

How to Align a Footer to the Bottom of the Page using Bootstrap 5

My issue arises when using a dark background footer, as some pages lack enough content to fill the entire browser's viewport. Consequently, an unsightly white band appears below it. How can I ensure the footer reaches the bottom of the viewport on pa ...

Leveraging a specific section of an HTML5 CSS3 Bootstrap template in a separate template

As I create my website using a combination of free templates, I often find myself needing to merge elements from different designs. One specific example is wanting to incorporate the "Client Logo Slider" component from template 2 into my original template. ...

What is the best way to prevent certain bootstrap classes from being applied across all elements?

After testing my HTML and CSS code on my local computer, it looks perfect. However, when I upload it to a server, the appearance changes. You can see it in action here: . The issue seems to be related to some bootstrap classes being activated differently ...

The table is too large for the parent div in which it is placed, causing

Check out this example I have for putting a table in a div and making it fill the div: ex1_jsfiddle table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; height: 100%; table-layout: fixed; } td, th { border: 1px sol ...

What is the best way to use ajax for uploading multiple images at once?

I'm having trouble uploading multiple image files. Can someone please review my code? <form id="fileupload" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="multiple" id="images_input"> & ...

Challenges with resizing floating divs

As a beginner in Web Development, I am tasked with creating a web portfolio for an assignment. In my design layout, I have a navigation bar in a div floated left, and a content div floated right serving as an about me section containing paragraphs and lis ...

`Apply a distinctive color to certain text within the selected text option`

                Does anyone have suggestions on how to create a color highlight with the word "stock" (see image for reference) using CSS, JavaScript, jQuery, or any other language? ...

Employing HTML and CSS to restrict the number of characters in sentences

Extracting a few sentences from a JSON file and displaying it in a <p> tag. <p> Thank you all for another wonderful night spent together this past Sunday at The Hippodrome in Baltimore. Thank yo... <p> I want to f ...

Styling the first visible item in ngRepeat using Angular

I am working with a list that is generated using ngRepeat, which looks like this <ul class="list-group"> <li class="list-group-item" ng-repeat="data in tree | filter:key"> {{data.name}} </li> </ul> My goal is to ma ...

Verify an ajax response by comparing it to the initial request

Is there a way to use ajax and/or jquery to verify that the response received is for the specific request sent? For clarification, I have a search page divided into two parts: the top section for search criteria and the bottom section for matched results. ...

Manipulating the 'display' attribute with jQuery

Is there a way to show an element that has been hidden using the following CSS? .pds-pd-link { display: none !important; } Can jQuery be used to enable the display of the .pds-pd-link CSS? For example, would $(.pds-pd-link).css("display",""); ...

Resolved issue with sticky header movement after scrolling event

I've been working on a sticky header code, but I'm having trouble achieving a smooth scroll transition. The fixed header seems to jump after just one scroll. Here is the basic HTML structure: <div class="headerWrapper"> <div id="to ...

Ensure that the accordion in jQuery remains open while navigating

I am currently developing a jQuery accordion feature where clicking on an 'li' element will both navigate and close the accordion. However, I need the accordion to stay open after navigating. Can someone please advise me on how to achieve this? T ...

Merge two Ajax calls that are triggered by different events

In my current setup, I have code in place to execute two separate ajax requests. The first one triggers on page load to display default content, while the second is activated when a user interacts with certain buttons, updating the content accordingly. I& ...

Issue: unhandled exception: Marketplace URI is not valid

It seems like I am the first one in this community to ask a question related to balancedpayments. I recently started working with balancedpayments. Here's what I've done so far: Set up a test marketplace Added a "webhoo ...

Displaying the scope in HTML from a custom Angular directive: A step-by-step guide

Just getting started with angular js and I have a question. Is there a way to show the values of e.width and e.height in an HTML document? Can I utilize the scope within this context? .directive( "cropping", [function ($scope) { return { rest ...