Web Analytics Made Easy -
StatCounter Alert box for form textarea - CodingForum

Announcement

Collapse
No announcement yet.

Alert box for form textarea

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Alert box for form textarea

    I was wondering if it's possible to use a javascript in a form textarea that will popup an alert box when a person reaches a designated number of words.

    Here for example is a js that launches an alert if the person exceeds 27 characters:
    Code:
    <INPUT TYPE="text" NAME="title" size="40" onKeyUp="if (this.value.length>27) { alert('Please limit your title to only 27 characters including blank spaces'); this.value=this.value.substring(0,27) }">
    It would be very useful to have something similar that counted the words, and when the maximum is reached, the alert displays. I've seen a number of js "word counting" scripts, but none that will use the alert the way I'm describing. Is that possible? Thanks...
    Reno CF

  • #2
    This may help:
    Code:
    <script type="text/javascript">
      function countIt(field,num) {
        var text = field.value;
        var word = text.split(/\s+/g);
          if(word.length>num){
             alert('You have reached the maximum number of words allowed');
             field.value = text.substring(0,(text.length-2));
          }
      }
    </script>
    </HEAD>
    
    <BODY>
    
    <form>
    <textarea name="area" COLS="75" ROWS="14" onkeyup="countIt(this,[color=red]10[/color])"></textarea>
    </form>
    .....Willy

    Comment


    • #3
      No need to use javascript here.
      RFTM: http://www.w3.org/TR/1999/REC-html40...ms.html#h-17.4
      and pay attention to maxlength property
      Vladdy | KL
      "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

      Comment


      • #4
        You do need to use scripting, maxlength sets the number of characters not words.

        Comment


        • #5
          Re: Alert box for form textarea

          I was referring to:
          Originally posted by Reno CF

          Here for example is a js that launches an alert if the person exceeds 27 characters:
          Code:
          <INPUT TYPE="text" NAME="title" size="40" onKeyUp="if (this.value.length>27) { alert('Please limit your title to only 27 characters including blank spaces'); this.value=this.value.substring(0,27) }">
          Sorry, did not make myself clear....
          Vladdy | KL
          "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

          Comment


          • #6
            Thanks very much Willy -- your script worked exactly as I had hoped. I appreciate you providing it fully developed as you did.

            Thanks too Vladdy for the w3.org forms instruction page -- I bookmarked it for ongoing reference....
            Reno CF

            Comment

            Working...
            X