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

Differences Between 'this' and 'self' in Classes

I am currently working with ES6 Classes and I'm struggling to grasp why I am able to access the this variable within one of the methods. //CODE class Form{ constructor(){ var self = this; } assemble(){ log(self); ...

Challenges with extracting and organizing dates using JavaScript regular expressions

My task involves organizing these text rows into specific groups based on certain criteria and names. Il Messaggero Roma 22 settembre 2023 Il Messaggero Roma 21 settembre 2023 Il Messaggero 22 settembre 2023 Il Messaggero 21 settembre 2023 Il Messaggero Ro ...

Automatically activate the next tab in Bootstrap

I have a modal that performs a count. Once the count is completed, the modal closes. My goal is to automatically switch to the next tab in Bootstrap when the modal closes. Here is the HTML: <ul class="nav nav-tabs namelocationtable"> <div clas ...

Difficulty with the Jquery <span> tag on Mozilla 3.6.1

I am currently utilizing jQuery.post to display some data retrieved from a servlet. <div class="Label"> <span id="result" >Click on Check.</span> </div> <script> $(document).ready(function() { $("a").c ...

What is the best way to trigger a controller action when the datepicker's value changes?

Hello, I have a custom datepicker and I am trying to perform a calculation when the year is changed. The code provided below does not seem to work on onchange. I also attempted using the onchange attribute and calling a JavaScript function like this oncha ...

Mapping fields in Spring to JSON can be tricky, especially when dealing with fields that can be

In my object, there is a String value field which can contain either true or false. The issue arises when Spring/Jackson maps this to value: "true" as a String, causing problems with certain angularjs ng-model mappings that expect a boolean. Is there a way ...

I'm interested in querying my mongodb collection data based on a particular field

I need help creating a list from my mongodb database that can be sorted by the "username" field. After learning about the limitations of namespaceing in mongodb, I'm trying to figure out how to sort my collection based on the "username" field. Here ...

How to message someone privately in a public Discord channel using discord.js

Can someone help me figure out how to create a message in discord.js version 12.5.3 that only I can see? I know how to send messages to channels using message.channel.send, but I'm not sure how to make a message visible only to myself. Thank you! ...

The data set in a setTimeout is not causing the Angular4 view to update as expected

I am currently working on updating a progress bar while importing data. To achieve this, I have implemented a delay of one second for each record during the import process. Although this may not be the most efficient method, it serves its purpose since thi ...

Accessing store in Vue, the getter function returns a value representing whether the user is currently logged

I have the user state stored in my Vue store, but when I try to access it like this: let isLoggedIn = store.getters.isLoggedIn Instead of getting a simple true or false, I see this in the console: ƒ isLoggedIn (state) { return state.user ? true : false ...

What is the best way to display an HTML page located in a subfolder with its own unique stylesheets and scripts using Express and Node?

I am looking to display an HTML page that is located within a subfolder along with its own unique style-sheets and scripts. I am using Express and Node for this purpose, and have already acquired a separate login page that I would like to render in a sim ...

What is the best way to update just the image path in the source using jQuery?

I am currently working on implementing a dark mode function for my website, and I have successfully adjusted the divs and other elements. However, I am facing an issue with changing the paths of images. In order to enable dark mode, I have created two sep ...

switch the visibility of the p tag based on its content

It seems like solving this shouldn't be too challenging, but I'm still learning when it comes to Javascript or JQuery. HTML: <p><span id="AddLine1Summary"></span>,</p> <p><span id="AddLine2Summary"></span& ...

Trouble displaying image due to issues with javascript, html, Angular, and the IMDb API integration

I have been working on displaying images from the IMDb API in my project. Everything works perfectly fine when I test it locally, but once I deploy the project to a server, the images do not load initially. Strangely, if I open the same image in a new tab ...

Navigating Vue 3's slot attributes: A step-by-step guide

Currently, I am facing an issue with a Vue component named MediaVisual. This component contains a slot. The problem arises when attempting to retrieve the src attribute of the slot element that is passed in. Surprisingly, this.$slots.default shows up as u ...

Angular BehaviorSubject is failing to emit the next value

I am facing an issue with a service that uses a Behavior subject which is not triggering the next() function. Upon checking, I can see that the method is being called as the response is logged in the console. errorSubject = new BehaviorSubject<any> ...

Sorting in Vuejs fails to function properly when a filter is applied

Having recently delved into Laravel and Vue, I am eager to develop a site for our Intranet. Pulling data from the Laravel database has been successful, displaying the data works well, and the search functionality is also up and running smoothly. However, I ...

Using infoWindows with multiple markers in Google Maps

i have developed a custom Google Maps application that pulls data from a CSV file. The functionality works well, but I am facing an issue with the infoWindow when looping through all the objects. It seems like the problem stems from the marker variable bei ...

Icons will be displayed when hovered over

Is it possible to display icons on hover in a div and hide them otherwise using just CSS? Or do I need to use javascript for this function? Thanks in advance! Example: HTML: <div id="text_entry"> <p>Some text here</p> <span ...

Ways to extend the timeout duration for Vercel Serverless functions

I've encountered an issue with my API endpoint on a NextJS project where it exceeds the 60-second time limit to execute. Despite being on a pro Vercel plan, I have been unable to extend the timeout limit successfully. Within the endpoint itself, I at ...