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 3675628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:59:58+00:00 2026-05-19T02:59:58+00:00

I’ve just started learning jQuery and already love it, but my skills aren’t enough

  • 0

I’ve just started learning jQuery and already love it, but my skills aren’t enough yet to solve this probably simple problem for my work: I have 3 different forms with an input field immediately followed by submit button:

alt text

I would like to prevent form submission if the text input field or the textarea before the submit button is empty. I’ve stolen a snippet from the jQuery Ninja book:

$(function() { 
        $(':submit').click(function(e) {
                $(':text').each(function() {
                        if ($(this).val().length == 0) {
                                $(this).css('border', '2px solid red');
                        }
                });
                e.preventDefault();
        });
});

But it has following problems:

  1. It ignores the textarea in the last form, because $(':text').each() does not apply to it
  2. It doesn’t allow submitting any form – I guess I need to move preventDefault() under an “if”
  3. I probably should better use prev() instead of each()
  4. If the user changes her mind and clicks another form, the previous form should be cleared

Could someone help me please? Alex

UPDATE:

I’ve tried:

$(function() {
        $(':submit').click(function(e) {
                $(this).prev(function() {
                        if ($(this).val().length < 2) {
                                // would be good to clear border for
                                // each text and textarea here...
                                $(this).css('border', '2px solid red');
                                e.preventDefault();
                        }
                });
        });
});

but unfortunately get 2 warnings:

Warning: Unexpected token in attribute selector: '!'.
Warning: Unknown pseudo-class or pseudo-element 'submit'.

and the red border doesn’t appear and the form is submitted when a button is clicked.

Also I’ve tried:

$(function() {
        $('form').submit(function(e) {
                $(':text, textarea').each(function() {
                        if ($(this).val().length < 2) {
                                $(this).css('border', '2px solid red');
                                e.preventDefault();
                        }
                });
        });
});

but the form is never submitted and all 3 text fields turn red instead of just one.

  • 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-19T02:59:58+00:00Added an answer on May 19, 2026 at 2:59 am
    <script type="text/javascript" src="/jquery-1.4.4.min.js"></script>
    <script type="text/javascript">
    $(function() { 
            $(':submit').click(function(e) {
                    $(':text, textarea').each(function() {
                            if ($(this).val().length == 0) {
                                    $(this).css('border', '2px solid red');
                                    e.preventDefault();
                            }
                    });
            });
    });
    </script>
    

    You’ll probably be ok with .each() 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.