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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:13:19+00:00 2026-06-17T12:13:19+00:00

I am trying to display an alert message when the input is shorter than

  • 0

I am trying to display an alert message when the input is shorter than given limit, using jQuery. Unfortunately, my code isn’t working, therefore, seeking your help.

Here are the codes I am using.

HTML

<input type="textarea" name="message" id="message" row="20" col="50" />
<input type="submit" name="submit" id="submit" value="Send Message" />

JavaScript

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $("input:submit[name='submit']").on("click",function() {
        var msgwords = $("input:textarea[name='message']").val().replace( /[^\w ]/g, "" ).split( /\s+/ ).length;
        var minwords = 10;
            if (comwords < minwords) {
                alert("Your Comment is too short.");
            }
        });
    });
</script>
  • 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-17T12:13:20+00:00Added an answer on June 17, 2026 at 12:13 pm

    You want to switch if(comwords < minwords) to if(msgwords < minwords)


    Here’s your fully working code. Change as you want.

    <script type="text/javascript">
    /* Use $ instead of `jQuery`, it's nicer */
    $(document).ready(function() {
        /* Reference the button ID (it's unique...) */
        $("#submit").click(function(e) {
            /* Your function to replace works nicely */
            var msgwords = $("#message").val().replace( /[^\w ]/g, "" ).split( /\s+/ ).length;
            var minwords = 10;
            if (msgwords < minwords) {
                e.preventDefault();
                alert("Your Comment is too short.");
            } 
        });
    });
    </script>
    

    This code references the exact ID’s (you can change this once you know it’s working). Your split function checking for words instead of characters also works nicely. Finally, e.preventDefault() prevents the button from submitting if the comment is too short.


    I’d also like to point out that you could use either e.preventDefault(); or return false, with the latter being the equivalent of e.preventDefault(); and e.stopPropagation();. This prevents that event from propagating (or “bubbling up”) the DOM.

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

Sidebar

Related Questions

i'm using MVC2, and am trying to display a friendly error message to the
I'm trying to display an alert message for the users in a catch block.
I'm trying to display an alert message when a user clicks on one of
I am trying to display an lengthy message in Default AlertDialog with following code.
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
I am currently using phonegap and trying to display results from a sqlite table
I was just trying to learn and understand jQuery source code (so far with
I'm trying to use the Phonegap notification to display an error message in my
I'm trying to get the content of the dt element using jquery. var test_name
I am trying to get data from a webserver and display a message saying

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.