Add a message displaying the error in the input field using jQuery Validation

Is there a way to append the jQuery Validation error messages to the input field or get help with positioning them properly?

The random popping up of error messages is causing chaos in the form layout.

I prefer to have the error messages displayed underneath each input field.

$(document).ready(function() {
  $('#register-form').validate({
    errorClass: 'error',
    rules: { //set rules for specific inputs
      name: {
        required: true,
        lettersonly: true
      },
      email: { //refers to the inputs name
        required: true,
        email: true
      },
      subject: {
        required: true,
        lettersonly: true,
      },
      workground: {
        required: true,
        lettersonly: true,
      },
      text: {
        required: true,

      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>
<form action="php/PHPMailer/send-mail.php" method="post" id="register-form">
  <input class="contact-input-type1" name="name" placeholder="Name" autocomplete="off">
  <input class="contact-input-type1" name="email" placeholder="E-Mail-Adresse" autocomplete="off">
  <input class="contact-input-type2" name="subject" placeholder="Anliegen" autocomplete="off">
  <input class="contact-input-type2" name="workground" placeholder="Metier" autocomplete="off">
  <textarea maxlength="300" class="contact-textarea" name="text" placeholder="Nachricht" autocomplete="off"></textarea>
  <input type="submit" class="contact-submit" value="Abschicken">
</form>

Answer №1

Here is a snippet for you:

$('#register-form').validate({
  errorClass: 'error',
  rules: { //specify rules for individual inputs
    name: {
      required: true,
      lettersonly: true
    },
    email: { //refers to the input's name
      required: true,
      email: true
    },
    subject: {
      required: true,
      lettersonly: true,
    },
    workground: {
      required: true,
      lettersonly: true,
    },
    text: {
      required: true,

    }

  }
});
label.error {
  float: none;
  color: red;
  padding-left: .5em;
  vertical-align: top;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<form action="php/PHPMailer/send-mail.php" method="post" id="register-form">
  <input class="contact-input-type1" name="name" placeholder="Name" autocomplete="off">
  <input class="contact-input-type1" name="email" placeholder="E-Mail-Adresse" autocomplete="off">
  <input class="contact-input-type2" name="subject" placeholder="Anliegen" autocomplete="off">
  <input class="contact-input-type2" name="workground" placeholder="Metier" autocomplete="off">
  <textarea maxlength="300" class="contact-textarea" name="text" placeholder="Nachricht" autocomplete="off"></textarea>
  <input type="submit" class="contact-submit" value="Abschicken">
</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

Transform HTML into a Vue component dynamically during runtime

Currently, I am in the process of learning Vue and working on a simple wiki page project. The raw article data is stored in a JSON file written using custom markup. For example, the following markup: The ''sun'' is {r=black black} shoul ...

Steps for navigating to a different page by clicking on a gridview row

Currently, I am utilizing a grid view on my webpage. My specific request is that upon clicking on any row within the grid, it should redirect to a separate page where all the details of the selected row will be displayed. Appreciate your assistance! ...

The existence of useRef.current is conditional upon its scope, and it may be null in certain

I'm currently working on drawing an image on a canvas using React and Fabric.js. Check out the demo here. In the provided demo, when you click the "Draw image" button, you may notice that the image is not immediately drawn on the canvas as expected. ...

Using a static function within a library's state function in NextJS is throwing an error: "not a function"

Inside a library, there's a special class known as MyClass. This class contains a static method named setData. The contents of the MyClass.js file are shown below: class MyClass { static DATA = ''; static setData(data) { MyClass ...

AngularJS enables you to easily manipulate image width and height using the ng-file-upload feature

Seeking assistance with validating image width and height based on a 1:3 ratio prior to uploading using ng-file-upload. The validation should occur before sending the image to the server. Unsure how to retrieve the dimensions of the selected image for val ...

using absolute URLs for image source in JavaScript

How can I output the absolute URLs of images in a browser window (e.g., 'www.mysite.com/hello/mypic.jpg')? Below is a snippet of my code: $('img').each(function() { var pageInfo = {}; var img_src = $(this).attr('s ...

When restarting the React application, CSS styles disappear from the page

While developing my React application, I encountered a problem with the CSS styling of the Select component from Material UI. Specifically, when I attempt to remove padding from the Select component, the padding is successfully removed. However, upon refre ...

Learn how to showcase a text file uploaded to a webpage with NODE js and HTML

<!DOCTYPE html> <html> <body> <form action = "/submit" method = "post"> Select a file: <input type="file" name="file"> <input type="submit"> </form> </bod ...

JS Issues with generating accurate dates in JS/JS Date perplexities

Creating a custom datepicker has been a challenging task for me. I'm facing difficulties in understanding how JS Date works and need some assistance to bridge this knowledge gap. The issue arises when I attempt to generate dates using a for loop, resu ...

Utilize load-grunt-configs and run bower.install() with grunt

I have been attempting to break down my current Gruntfile into smaller parts for better clarity. However, I have encountered a hurdle while using bower.install to manage project dependencies. My directory structure appears as follows: package.json bower.j ...

An elaborate warning mechanism in Redux-observable that does not trigger an action at the conclusion of an epic

I'm currently working on implementing a sophisticated alert system using redux and redux-observable. The requirements are: An action should request an alert: REQUEST_ALERT An action should create an alert and add an ID: SET_ALERT (handled in the ep ...

Issue with Fancybox Buttons not appearing on parent page upon opening

I previously asked for help on how to open fancybox from an iframe to the parent page. The solution worked perfectly and is still functioning well. Now, I've been requested to add left and right arrow buttons, but it seems that using the code to open ...

Steps for integrating WebRTC recording functionality with a Node.js server

Looking to develop a user-friendly video blogging solution that utilizes WebRTC technology for direct recording of video/audio within the browser, similar to Youtube's My_Webcam. The backend should be powered by Node.js. While exploring various Node. ...

Utilizing AJAX for XML data parsing

I need help with formatting XML data into a table. The code I've written isn't working as expected. The XML data is structured in branches, causing it to not display correctly. Can someone assist me in fixing this issue? <!DOCTYPE html> &l ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

Refreshing div content based on dropdown selection without reloading the page

I am currently working on implementing a dynamic dropdown feature that will update text content on a webpage without needing to refresh the entire page. The updated text will be fetched from a PHP function which receives input from the dropdown selection. ...

Are the functionalities of twilio-common.js on github equivalent to those of twilio-client.js on their CDN?

Currently, I am integrating the Twilio SDK client from the twilio CDN using this link: //media.twiliocdn.com/sdk/js/client/v1.4/twilio.min.js However, I am interested in importing the package via npm due to some restrictions. The only option I see availa ...

Is using the new Date function as a key prop in React a good

In my React code, I have been using new Date().getTime() as key props for some Input components. This may not be the best practice as keys should ideally be stable. However, I am curious to know why this approach is causing issues and why using Math.random ...

Would you say the time complexity of this function is O(N) or O(N^2)?

I am currently analyzing the time complexity of a particular function. This function takes a string as input, reverses the order of words in the string, and then reverses the order of letters within each word. For example: “the sky is blue” => “eu ...

Forwarding the geographic coordinates directly to the system's database

I have a unique script that retrieves the precise latitude and longitude position. It then automatically sends this data to a database without the need for user input. <script> function getPosition(position) { var latitude = position.coor ...