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

  • Home
  • SEARCH
  • 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 7431763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:20:46+00:00 2026-05-29T09:20:46+00:00

I’ve build pretty simple validator that will display my custom error message when someone

  • 0

I’ve build pretty simple validator that will display my custom error message when someone tries to submit empty form. But I have some questions.

I have .each() loop on :input elements – how can I make it loop through :input and textarea?

I use $(this).parent() to get the form object of input, but how can I make sure that it is form, not some other element like div?

Code with comments

$('form input[type=submit]').click(function(){

    // First we get the form class
    var form = $(this).parent(); // How can I make sure that the form is selected, not some other parent like div?
    var formClass = $(form).attr('class');

    // Then remove every previous messages on this form
    $('.' + formClass + ' .validation-error').each(function(){
        $(this).remove();
    });

    // Iterate through every input to find data that needs to be validated
    $('.' + formClass + ' :input').each(function(){ // How can I make this work with textareas as well as inputs without copying this whole each loop?

        // If it is marked as required proceed
        if ($(this).attr('required') == 'required'){

            // Getting current text and placeholder text
            var currentText = $(this).val();
            var placeholderText = $(this).attr('placeholder');

            // Replacing spaces to avoid empty requests
            currentText = currentText.replace(' ', '');
            placeholderText = placeholderText.replace(' ', '');

            // If current text is empty or same as placeholder proceed
            if (currentText == '' || currentText == placeholderText){

                // Get input position in order to place error message
                var inputPos = $(this).position();
                var left = inputPos.left + 200;
                var top = inputPos.top - 4;

                // Generate error message container and error message itself
                var errorMsg = '<div class="validation-error" style="position:absolute;left:' + left + 'px;top:' + top + 'px;"><- This</div>';

                // Appending error message to parent - form
                $(this).parent().append(errorMsg);
            }
        }
    });
});
  • 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-29T09:20:47+00:00Added an answer on May 29, 2026 at 9:20 am

    Question 1:

    I have .each() loop on :input elements – how can I make it loop through :input and textarea?

    Answer:

    $(':input, textarea').
    

    Question 2:

    I use $(this).parent() to get the form object of input, but how can I make sure that it is form, not some other element like div?

    Answer:

    $(this).closest('form')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.