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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:18:38+00:00 2026-05-28T14:18:38+00:00

I would like to check if a text field in the post form is

  • 0

I would like to check if a text field in the post form is empty before posting using jquery. If any, I would like to display an error message right next to the text area. I click the button to submit the information with empty fields but there are no messages displayed. I need this check function before the posting is perform. Here is what I have done.

<!DOCTYPE html>
    <html>
    <head>
    <script src="jquery-1.7.1.min.js"></script>
    <script>
    function ValidateForm()
    {
        $(document).ready(function(
              {
                  $("#personID").each(function()
                       {
                           if(this.val()=="")
                           {
                                $("#error_msg").html("Field needs filling");
                           }              
                       }      
              }
        ));

    }


    </script>
    </head>
    <body>
    <form action="action.php" method="post" id="personID">
    First Name: <input type="text" name="first"><span id="error_msg"></span></br>
    Last Name: <input type="text" name="last"><span id="error_msg"></span></br>
    Phone: <input type="text" name="phone"><span id="error_msg"></span></br>
    Mobile: <input type="text" name="mobile"></br>
    Position: <input type="text" name "pos"><span id="error_msg"></span></br>
    <input type="button" value="Insert" onclick="ValidateForm">
    </form>
    </body>
    </html>
  • 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-28T14:18:38+00:00Added an answer on May 28, 2026 at 2:18 pm

    The function should be

    function ValidateForm()
    {
        $('span.error_msg').html('');
       var success = true;
        $("#personID input").each(function()
            {
                if($(this).val()=="")
                {
                    $(this).next().html("Field needs filling");
                    success = false;
                }
        });
        return success;
    }
    

    and your form should be

    <form action="action.php" method="post" id="personID" onsubmit="return ValidateForm();">
        First Name: <input type="text" name="first"><span class="error_msg"></span></br>
        Last Name: <input type="text" name="last"><span class="error_msg"></span></br>
        Phone: <input type="text" name="phone"><span class="error_msg"></span></br>
        Mobile: <input type="text" name="mobile"></br>
        Position: <input type="text" name "pos"><span class="error_msg"></span></br>
        <input type="submit" value="Insert">
    </form>
    

    You didn’t added any span next to your phone textbox and if it remains empty the form won’t be submitted and it won’t display error message as well, so be sure about it. If you want to leave it optional then add a class to it (class='optional') and change the function if($(this).val()=="") to if($(this).val()=="" && !$(this).hasClass('optional')). that’s it.

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

Sidebar

Related Questions

I would like to perform form validation using JavaScript to check for input field
I would like to include license-check into my NSIS installer. Simple Text field and
i would like to check that whether the field entered in the text field
I would like to be able to check a string of text and extract
I would like to check which type of RAM my computer uses before I
I would like to check if an object is defined or exists using C#.
I have a simple issue -- I would like to check a field to
I would like to know if my <input type=text> field is changed during edition,
I would like to make a facebook page tab with a form field, where
I would like to check my JavaScript files without going to JSLint web site.

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.