Software, your way.
burger menu icon
WillMaster

WillMasterBlog > JavaScript

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Preventing Excessively Large Feedback/Contact Messages

Very large emails and feedback/contact messages are more likely to be ignored than short, succinct ones. Zenhabits' Your Emails are Too Long makes good arguments in support of short written messages.

I'll show you how to display a custom error message if the number of characters in the form user's message exceeds your specified limit. If the limit is not exceeded, there is no indication there was a limit at all.

Another way to limit the message length is to put word or character counters next to the form so the form user can see when the message is near maximum length. And don't let the counter exceed that limit.

WebSite's Secret members have a Form Field Word and Character Limits generator they can use to do exactly that. (The link is to the public article.)

Placing word or character counters near the form's message box may be distractive to the form user. If that is a concern, displaying an error message only if the message is excessive can be a better solution.

Here is how to do it.

The First Step

Put this JavaScript somewhere in the source code of the web page with the form. It may be in the HEAD or BODY area.

<script type="text/javascript">
function CheckMaximumFieldLength(field) {
/* 
   Maximum Field Length Message
   Version 1.0
   April 14, 2011

   Will Bontrager
   https://www.willmaster.com/
   Copyright 2011 Bontrager Connection, LLC

   This software is provided "AS IS," 
   without a warranty of any kind. 
   Bontrager Connection, LLC grants 
   you a royalty free license to use 
   or modify this software provided 
   this notice appears on all copies. 
*/
///////////////////////////////////////
//
// Two places to customize:
//
// Place 1.
// Specify the maximum number of characters 
//   (including spaces) the field may contain.

var MaximumCharacters = 300;

// Place 2.
// Specify the message to display if the maximum is exceeded.

var Message = "Please reduce your message to under 300 characters."

// No further customizations required.
///////////////////////////////////////
if(document.getElementById(field).value.length<=MaximumCharacters) { return true; }
alert(Message);
return false;
}
</script>

The JavaScript has two places to customize, the maximum number of characters and the message to display if the maximum is exceeded. Both customization places are marked.

If your message will be more than one line, see the 3 Backslash Uses in JavaScript Strings blog post.

The Last Step

The form needs to be modified in one or two places to use the above JavaScript.

  1. The text box to be checked for length needs an id value. If it already has an id value, there is no need to change it. Otherwise, give it one.

    The id value might look like this: id="messageboxid"

    Here is an example of a textarea field with an id value:

    <textarea id="messageboxid" name="message" cols="33" rows="5"></textarea>
    
    
  2. The form tag needs this attribute:

    onsubmit="return CheckMaximumFieldLength('IDVALUE')"
    
    

    Replace IDVALUE with the id value of the field to be checked. For the above example textarea field, the form tag attribute would be:

    onsubmit="return CheckMaximumFieldLength('messageboxid')"
    
    

    With the onclick attribute, the form tag might look something like this:

    <form 
       method="post" 
       action="script.php" 
       onsubmit="return CheckMaximumFieldLength('messageboxid')">
    
    

Implementation Review

To implement the maximum field length message system, put the JavaScript into the page, then verify the text field to check has an id value and give the form tag the onsubmit attribute.

When the form is submitted, the field length is checked. If the field length less or equal to the maximum specified in the JavaScript, the form submits normally. Otherwise, the message specified in the JavaScript is displayed and the form submission is aborted.

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Extracting Dates From the Unix Timestamp

The UNIX timestamp can be used to determine the represented time for any time zone on Earth.

Submit Form Data Without Bothering User

To silently and effectively submit a form on a web page, Ajax with FormData() can be used.

CSS Dots

CSS dots can be used for interactions between web page and website user.

Email Testing

One thing to test when email doesn't get sent or doesn't arrive is whether the server actuall sends email.

Tap to Select

Find out how to make a link or button to select the content in a div or other HTML element.

Capitalizing the First Letter of Words

Here, find both PHP code and JavaScript code to capitalize the first letter of a word.

Fixed-position Table Header

During a vertical scroll, the table header scrolls out of view -- unless the header is fixed in position.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC