Limit textarea alert message

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • teser3@hotmail.com

    Limit textarea alert message


    I have the below that limits the textarea input to 500 characters but
    cant get the alert message to work. It doesnt show anything. Please
    advise.
    <script language="javas cript" type="text/javascript">
    function limitText(limit Field, limitCount, limitNum) {
    if (limitField.val ue.length limitNum) {
    limitField.valu e = limitField.valu e.substring(0, limitNum);
    alert("You are trying to enter more
    than 500 characters");
    } else {
    limitCount.valu e = limitNum - limitField.valu e.length;
    }
    }
    </script>
    --------------------------------------------------------------------------------

    <form name="myform">
    <textarea name="limitedte xtarea"
    onKeyDown="limi tText(this.form .limitedtextare a,this.form.cou ntdown,
    500);"
    onKeyUp="limitT ext(this.form.l imitedtextarea, this.form.count down,
    500);">
    </textarea>
  • Lasse Reichstein Nielsen

    #2
    Re: Limit textarea alert message

    "teser3@hotmail .com" <teser3@hotmail .comwrites:
    I have the below that limits the textarea input to 500 characters but
    cant get the alert message to work. It doesnt show anything.
    Do you get any error messages from the browser?

    Does the line previous to the alert work? I.e., is the textarea
    contents restricted to 500 chars?
    alert("You are trying to enter more
    than 500 characters");
    The linebreak is not allowed in a string. If this is your actual code,
    that could be the problem.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Evertjan.

      #3
      Re: Limit textarea alert message

      teser3@hotmail. com wrote on 07 mei 2008 in comp.lang.javas cript:
      >
      I have the below that limits the textarea input to 500 characters but
      cant get the alert message to work. It doesnt show anything. Please
      advise.
      <script language="javas cript" type="text/javascript">
      function limitText(limit Field, limitCount, limitNum) {
      if (limitField.val ue.length limitNum) {
      limitField.valu e = limitField.valu e.substring(0, limitNum);
      alert("You are trying to enter more
      than 500 characters");
      beware of linebraks!!!
      } else {
      the else is not needed.
      limitCount.valu e = limitNum - limitField.valu e.length;
      }
      >}
      </script>
      -----------------------------------------------------------------------
      ---------
      >
      <form name="myform">
      <textarea name="limitedte xtarea"
      onKeyDown="limi tText(this.form .limitedtextare a,this.form.cou ntdown,
      500);"
      onKeyDown="limi tText(this,this .form.countdown , 500);"

      does the same.
      [If you are John,
      saying "the son of my father named John" is in effect
      the same as saying "me".]

      The onkeyup in fact is all that is needed.
      onKeyUp="limitT ext(this.form.l imitedtextarea, this.form.count down,
      500);">
      </textarea>
      This works fine:

      =============== ===
      <script type='text/javascript'>
      var max = 500; // or debug with 5
      function limitText(limit Field, limitCount, limitNum) {
      if (limitField.val ue.length limitNum) {
      limitField.valu e = limitField.valu e.substring(0, limitNum);
      alert('Do not enter more than '+ limitNum +' characters');
      };
      limitCount.valu e = limitNum - limitField.valu e.length;
      };
      </script>

      <form name='myform'>
      <textarea name='limitedte xtarea'
      onKeyUp='limitT ext(this,this.f orm.countdown,m ax);'>
      </textarea>
      <br><br><inpu t name='countdown ' readonly>
      </form>
      =============== ====

      Well, not perfect, if you add letters in the middle, another letter will
      be deleted, the one at the end.

      And not perfect, because you can add a long sentence by pasting [ctrl-V]

      So additional tests have to be done, preferably onsubmit [and also
      serverside testing, if Javascript is switched off, or if the clientside
      code is manipulated.]

      Depending on the OS, a <returnwill count for two, or one character.

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Evertjan.

        #4
        Re: Limit textarea alert message

        Evertjan. wrote on 07 mei 2008 in comp.lang.javas cript:
        This works fine:
        >
        =============== ===
        <script type='text/javascript'>
        var max = 500; // or debug with 5
        function limitText(limit Field, limitCount, limitNum) {
        if (limitField.val ue.length limitNum) {
        limitField.valu e = limitField.valu e.substring(0, limitNum);
        alert('Do not enter more than '+ limitNum +' characters');
        };
        limitCount.valu e = limitNum - limitField.valu e.length;
        >};
        </script>
        >
        <form name='myform'>
        <textarea name='limitedte xtarea'
        onKeyUp='limitT ext(this,this.f orm.countdown,m ax);'>
        </textarea>
        <br><br><inpu t name='countdown ' readonly>
        </form>
        =============== ====
        >
        Well, not perfect, if you add letters in the middle, another letter
        will be deleted, the one at the end.
        >
        And not perfect, because you can add a long sentence by pasting
        [ctrl-V]
        >
        So additional tests have to be done, preferably onsubmit [and also
        serverside testing, if Javascript is switched off, or if the
        clientside code is manipulated.]
        >
        Try this, IE tested, the oncontextmenu seems IE specific:

        =============== =
        <script type='text/javascript'>
        var max =10;
        function limitText(limit Field, limitCount, limitNum) {
        if (limitField.val ue.length limitNum) {
        limitField.valu e = limitField.old;
        alert('Do not enter more than '+ limitNum +' characters');
        };
        limitCount.valu e = limitNum - limitField.valu e.length;
        limitField.old = limitField.valu e;
        };
        </script>

        <form name = 'myform'>
        <textarea name = 'limitedtextare a'
        onkeydown = 'this.old=this. value;'
        onKeyUp = 'limitText(this ,this.form.coun tdown,max);'
        oncontextmenu = 'alert("No pasting please");return false;'>
        </textarea>
        <br><br><inpu t name='countdown ' readonly>
        </form>
        =============== =

        Ctrl-V is only allowed if the result is within limit.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        Working...