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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:17+00:00 2026-06-09T21:10:17+00:00

I am just trying to print a error message if someone leaves the text

  • 0

I am just trying to print a error message if someone leaves the text field blank using jquery.But it is not being displayed.
This is html code:

 <form id="form1" runat="server">
    <div>
    <table>
     <tr>
    <td>
   <label>Name</label></td>
   <td> <input class="required" type="text" name="name" id="name" /><span>(required)</span></td>
     </tr><tr>
    <td><br/><label>Address</label></td>
    <td><input class="required" type="text" name="address" id="address"/><span>(required)</span></td></tr>
     </table>
    </div>
    </form>

This is Jquery code.

$(document).ready(function () {
   var requiredFlag = ' * ';
    $('form :input').filter('.required').next('span').text(requiredFlag).end();
    if ($(this).is('.required')) {
       if (this.value == '') {
       var errorMessage = 'This is a required field';
                             };
            $('span></span').text(errorMessage).appendTo($(this));
                             }; 
});

May be i am missing something.
Help experts.
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-06-09T21:10:18+00:00Added an answer on June 9, 2026 at 9:10 pm
    1. this in your code refers to window/document object not the input elements;
    2. There are some typos in your code => $('span></span');
    3. There is no event handler in your code for validating the inputs
    4. You have span elements in your markup and there is no need for generating a new one
    5. You can use the form submit event for validating the inputs
    6. The :input selector has been deprecated

    Try this:

    var errorMessage = 'This is a required field';
    $('form input.required').next().text('*');
    
    $('form').submit(function(){
       var errors = 0;
       $('input.required', this).each(function(){
           if (this.value == '') {
              $(this).next().text(errorMessage);
              errors++;
           }
       })
       if (errors > 0) {
         return false // prevents the default action of the event if there are errors
       }
    })
    
    1. submit()
    2. next()
    3. each()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print formatted xml to a file but my XmlNodePrinter just
just trying to test for equality in this piece of code, but getting a
I was just trying to learn and understand jQuery source code (so far with
I'm trying to do the search tutorial but I got an error where it
I am trying to exit a python script without the annoying error message coming
I am trying to read a .gz file and print the text content on
I was trying to paint border around JLabel when it is clicked. Just like
I am trying to connect QWebpage::windowCloseRequested() to a slot that just prints out a
Just trying to get diff to work better for certain kinds of documents. With
Just trying to still get my head around IOC principles. Q1: Static Methods -

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.