Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:35+00:00 2026-05-23T12:13:35+00:00

I am writing a basic jQuery form validation script and I have something like

  • 0

I am writing a basic jQuery form validation script and I have something like this going on:
(Please note, I have simplified the validation criteria since it is not the problem here.)

$('#contact-form').submit( function (event) {
    event.preventDefault();

    var name = $(this).find('input[type="text"]').val();
    var message = $(this).find('textarea').val();

    //  Validation
    if (name.length <= 3 || message.length <= 3) {
        $(this).after('<span>Error<span>');
    }
}); 

What I wnat to do is somhow make jQuery recognize the corect this where the error has ocured from the

if (name.length <= 3 || message.length <= 3) {
   $(this).after('<span class="error">Error<span>');
}

statement and insert the error span after it. What I currently have inserts the error message only after the first input, not the input that had a mistake.

I know how to do it if I separate each element validation in its own if statement, but I think that would be unnecessary repetition.

Also please don’t suggest any validation plugins, I am aware of those, but I want to learn to do this on my own.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:13:36+00:00Added an answer on May 23, 2026 at 12:13 pm

    This in jquery refers to the element that generated the event: in your case $(this) refers to the element with id = ‘contact.form’.

    You could do something like this:

    $('#contact-form').submit( function (event) {
        event.preventDefault();
    
        $('input[type=text],textarea').each(function(){
              //in this case this refers to the current iterated object
              var value = $(this).val();
              if (value.length < 3){
                  $(this).after('<span>Error - too short<span>');
              }
    
        });
    
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I was just writing another basic script for my JS & jQuery practice,
I'm writing a basic planet viewer OpenGL Perl application, just for fun. I have
I'm currently writing some methods that do some basic operations on form controls eg
I'm actually writing an MPI program. This is a basic client / server pattern.
I got this doubt while writing some code. Is 'bool' a basic datatype defined
I'm writing a jQuery plugin and I would like my code to take advantage
I am writing a basic game engine and have an abstract class that represents
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I'm writing a unit test with Boost.Unit, and I would like to include basic
I'm writing a basic SQLite wrapper. While doing this I noticed I'm frequently manually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.