Click here to Skip to main content
14,271,278 members

How put alert message to text maximum javascript

Member 12895537 asked:

Open original thread
Hello. I have a question regarding my codes. My question is "i want to put alert message if that person exceed words limitation". my code is function well and when exceed that person cannot type, however which part on javascript I want to put popup the alert message if he/she exceed?

What I have tried:

<textarea class="max" name="coverLetterText" id="coverLetterText" cols="80" rows="12" background placeholder="Make a Pitch not more than 400 words">

<pre><!-- script untuk textarea max-->  
<script>jQuery(document).ready(function($) {
    var max = 400;
    $('textarea.max').keypress(function(e) {
        if (e.which < 0x20) {
            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return;     // Do nothing
        }
        if (this.value.length == max) {
            e.preventDefault();
        } else if (this.value.length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
        }
    });
}); //end if ready(fn)</script>
Tags: Javascript, HTML, Text, MessageBox, AlertDialog

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100