Customizing error styles in a table using Jquery validation

My form is using JQuery .validation().

Here is the structure of my form:

<form....>
<table cellspacing="0" cellpadding="0">
<tr>
<td>Name: </td>
<td><input type='text' name='Name'/></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type='text' name='LastName'/></td>
</tr>
<tr>
<td>Address: </td>
<td><input type='text' name='Address'/></td>
</tr>
</table>
<input type='submit' name='enter' value='submit'/>
</form>

Can I display errors like this in my form?

Your help is greatly appreciated!

Answer №1

Check out the live demo here

HTML Form Structure

<form name="userDetails">
<table cellspacing="1" cellpadding="1"&tg;
<tr>
<td>Name: </td>
<td><input type='text' name='Name'/>
    <span class="error_span">Please enter your name</span>
    </td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type='text' name='LastName'/>
        <span class="error_span">Please enter your last name</span>
    </td>
</tr>
<tr>
<td>Address: </td>
<td><input type='text' name='Address'/>
        <span class="error_span">Please enter your address</span>
    </td>
</tr>
</table>
<input type='button' id='submitForm' name='enter' value='Submit'/>
</form>

jQuery Validation Script

$(document).ready(function(){
    $("#submitForm").click(function(){
        $('input').each(function(index) {
                   if($(this).val()=="") {
                       $(this).addClass("has_error");
                       $(this).siblings(".error_span").show();
                   }else{
                       $(this).removeClass("has_error");
                       $(this).siblings(".error_span").hide();
                   }
          });
    });
 });

CSS Styling for Error Messages

.error_span{
color:red;
    display:none;
}

.has_error{
 border:1px solid red;
}

View the demo on JSFiddle

To implement form validation, use the onsubmit attribute in the form element to run the JavaScript code and prevent submission if there are validation errors.

Answer №2

There are two different approaches to accomplish this task:

1)

<form....>
<table cellspacing="0" cellpadding="0">
<tr>
<td>Name: </td>
<td><input type='text' name='Name'/></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type='text' name='LastName'/></td>
</tr>
<tr>
<td></td>
<td>Name Is Required</td>
</tr>
<tr>
<td>Address: </td>
<td><input type='text' name='Address'/></td>
</tr>
</table>
<input type='submit' name='enter' value='submit'/>
</form>

or

2)

<form....>
    <table cellspacing="0" cellpadding="0">
    <tr>
    <td>Name: </td>
    <td><input type='text' name='Name'/></td>
    </tr>
    <tr>
    <td>Last Name: </td>
    <td><input type='text' name='LastName'/> <br />Name Is Required</td>
    </tr>
    <tr>
    <td>Address: </td>
    <td><input type='text' name='Address'/></td>
    </tr>
    </table>
    <input type='submit' name='enter' value='submit'/>
    </form>

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

How can I feature an image at the top of the page with large 3 and jQuery in FireFox?

I am interested in showcasing a single image at the forefront of the page when it is selected. While there are numerous plug-ins that offer this feature, many come with unnecessary extras like galleries, video support, and thumbnails which I do not requir ...

Creating a draggable element in JavaScript using React

I've been attempting to create a draggable div element, but I'm encountering some perplexing behavior. The code I'm using is directly from w3schools and it functions correctly for them, however, in my implementation, the div always shifts to ...

Converting HTML to plain text - unclear source encoding

I am currently working on a project involving PHP where I extract HTML content from websites, convert them into plain text, and store them in a database. The challenge I am facing is that I do not know the original encoding of the content. What would be t ...

Clearing hoverIntent using jQuery

In my scenario, I have three items identified as X, Y, and Z. The functionality of hovering is controlled by the hoverIntent. When I hover on item X, a tooltip is displayed using the following code: jQuery('.tooltiper').hoverIntent({ ove ...

Unable to locate module during deployment to Vercel platform

I have developed a website using NextJS. It functions perfectly when I run it through npm run dev, but when I try to build and deploy it on Vercel, I encounter an error stating that it cannot find the module. However, the module is found without any issues ...

Exiting the Protractor timeout setting for Safari WebDriver

While I have experience with unit tests using Karma, my office is now interested in integration tests, specifically to test cross-browser capabilities. Protractor seemed like the best option for this, so I began working on some basic dashboard tests. Howev ...

Wait for animation to finish before executing ajax call in jQuery

When I upload a spreadsheet to import data into my database, I perform server-side validation on each value before insertion. Using jQuery AJAX, the file is uploaded to the server, and I receive back processed data in the form of a multidimensional array. ...

Removing an element in JSON is resulting in an element that is undefined

Dealing with a JSON Object, I am faced with a challenge where deleting elements results in undefined entries, causing issues in my usage within datatables. Here is my current approach: function dTable(presId){ var allregos = '[{"presId": "a09N0000 ...

Change the CSS dynamically

I'm curious about how to create a similar effect like the one on this page: On the left side, there is a panel that allows for adjusting the template. What I've noticed is that when I change the color, different CSS files are used (blue.css, pur ...

Tips for stopping the textarea from moving around as you type and avoid it hitting the bottom of the page: Utilize JQuery and

As I type into the auto-sizing textarea, the text runs to the bottom of the page and causes it to dance uncontrollably with each key press. This forces me to constantly scroll down to see what's happening. How can I prevent this page dancing and keep ...

Error in Typescript: A computed property name must be one of the types 'string', 'number', 'symbol', or 'any'

Here is the current code I am working with: interface sizes { [key: string]: Partial<CSSStyleDeclaration>[]; } export const useStyleBlocks = ( resolution = 'large', blocks = [{}] ): Partial<CSSStyleDeclaration>[] => { cons ...

Importing textures and loading mtl files in Three.js from various web addresses

Something strange is happening with my Roblox API integration. I'm trying to retrieve the OBJ file, MTL file, and Texture file using the API. The main API link I have is https://t2.rbxcdn.com/ef63c826300347dde39b499e56bc874b, which leads me to two cru ...

"Enjoy a unique browsing experience with a two-panel layout featuring a fixed right panel that appears after scrolling

I am facing difficulty in designing a layout with two panels where the left panel has relative positioning and the right panel becomes fixed only after a specific scroll point. Additionally, I need the height of the right panel to adjust when the page scro ...

Differences between urlencoded and form-data in Node.js

Can anyone suggest a fast method to distinguish between passing urlencoded data and form-data (multiparty) data in Node.JS? ...

Generate dynamic Bootstrap Carousel slides with unique hashtag URLs

I've been attempting to connect to various slides within a bootstrap carousel from a different page but have had no success. Here is an example of what I'm trying to achieve: <a href="services#slide2">Link to Slide 2</a> For refere ...

The Google Visualization chart fails to display properly once CSS is applied

Struggling with a graph display issue here. It's quite perplexing as it works fine on older laptops and Safari, but not on Chrome or older versions of Firefox. Works like a charm on my old laptop and Safari, but fails on Chrome and Firefox (haven&apo ...

Adding data from PHP into a designated div element

update: After some trial and error, I managed to find a solution using jQuery and append() methods. From my PHP code, I created an array containing all the necessary information that I wanted to insert into the tab divs. I then encoded this array into JSON ...

Setting up craco for jsx in your project

I am currently utilizing craco and grappling with how to configure jsx. I keep encountering the error below: Support for the experimental syntax 'jsx' isn't currently enabled (4:17): The suggestion is to add `babel/preset-react or utilize ...

Invoke v-on:click once new HTML has been added in Vue.js

Is it possible to trigger a function onclick after appending HTML in Vue.js using vue-append? this.html = '<div id="'+folderData[key].id+'" class="col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2">' + ...

Loading a PDF file in a dynamic manner

I'm having trouble with the syntax for passing PDF data received from an AJAX request and updating an object with the PDF data. I'm attempting to implement a progress meter / please wait mechanism while the PDF is being loaded from the server, b ...