maxlength on multiline textbox

Discussion in 'ASP .Net' started by hardcoded, May 5, 2004.

  1. hardcoded

    hardcoded Guest

    I have a textbox with TextMode set to MultiLine. I also have the MaxLength
    set to 255. This maxlength value seems to get ignored as the user can enter
    unlimited characters. Does the multiline textbox not support maxlength?
    How can I make the work?

    TIA
    hardcoded, May 5, 2004
    #1
    1. Advertising

  2. No the max length doesn't work as the html tag rendered is a textarea tag,
    which has no MaxLength property.

    You will need to use a Validator control to ensure the length doesn't exceed
    the MaxLength. You could use a RegExValidator or derive your own from
    CustomValidator.

    HTH,

    bill


    "hardcoded" <> wrote in message
    news:%...
    > I have a textbox with TextMode set to MultiLine. I also have the

    MaxLength
    > set to 255. This maxlength value seems to get ignored as the user can

    enter
    > unlimited characters. Does the multiline textbox not support maxlength?
    > How can I make the work?
    >
    > TIA
    >
    >
    William F. Robertson, Jr., May 5, 2004
    #2
    1. Advertising

  3. hardcoded

    hardcoded Guest

    Just as I suspected. Thanks!

    "William F. Robertson, Jr." <> wrote in message
    news:...
    > No the max length doesn't work as the html tag rendered is a textarea tag,
    > which has no MaxLength property.
    >
    > You will need to use a Validator control to ensure the length doesn't

    exceed
    > the MaxLength. You could use a RegExValidator or derive your own from
    > CustomValidator.
    >
    > HTH,
    >
    > bill
    >
    >
    > "hardcoded" <> wrote in message
    > news:%...
    > > I have a textbox with TextMode set to MultiLine. I also have the

    > MaxLength
    > > set to 255. This maxlength value seems to get ignored as the user can

    > enter
    > > unlimited characters. Does the multiline textbox not support maxlength?
    > > How can I make the work?
    > >
    > > TIA
    > >
    > >

    >
    >
    hardcoded, May 5, 2004
    #3
  4. hardcoded

    WJ Guest

    "hardcoded" <> wrote in message
    news:...
    > Just as I suspected. Thanks!
    >


    Not much you can do on any web-based application. Price to pay for ! :)

    John
    WJ, May 5, 2004
    #4
  5. hardcoded

    i-Safire Guest

    try the this

    <HTML>
    <HEAD>
    <SCRIPT>
    function fun_pTextarea_MaxLen(prm_oTextarea_Obj)
    { var var_sKey_Code, var_aKey_Special, var_bResult,
    var_oTextarea_TxtRng;

    var_oTextarea_TxtRng = prm_oTextarea_Obj.createTextRange();
    var_aKey_Special = [8,17,18,27,33,34,35,36,37,38,39,40,45,46,114];
    var_bResult = true;
    var_sKey_Code = event.keyCode;
    if (var_sKey_Code == 86)
    { if (event.ctrlKey) var_bResult =
    fun_mTextarea_Paste(prm_oTextarea_Obj); }

    if(prm_oTextarea_Obj.value.length >= prm_oTextarea_Obj.maxLen)
    { var_bResult = false;

    if (var_oTextarea_TxtRng.queryCommandState('OverWrite') &&
    (prm_oTextarea_Obj.value.length == prm_oTextarea_Obj.maxLen))
    { var_bResult = true; }
    else
    {
    for (i=0; i<var_aKey_Special.length; i++)
    { if (var_sKey_Code == var_aKey_Special) {var_bResult = true;
    break;} }
    }
    }

    return var_bResult;
    }

    function fun_mTextarea_Paste(prm_oTextarea_Obj)
    { var var_sClipboard_Text;
    var_sClipboard_Text = window.clipboardData.getData("Text");
    prm_oTextarea_Obj.TxtRng =
    document.selection.createRange().duplicate();

    if (prm_oTextarea_Obj.TxtRng && prm_oTextarea_Obj.createTextRange)
    { prm_oTextarea_Obj.TxtRng.text =
    prm_oTextarea_Obj.TxtRng.text.charAt(prm_oTextarea_Obj.TxtRng.text.length
    - 1) == ' ' ? var_sClipboard_Text + ' ' : var_sClipboard_Text;
    }
    else
    { prm_oTextarea_Obj.TxtRng.text = var_sClipboard_Text;}

    prm_oTextarea_Obj.value = prm_oTextarea_Obj.value.substring(0,
    prm_oTextarea_Obj.maxLen);
    return false;
    }

    </SCRIPT>
    </HEAD>
    <BODY>
    <textarea maxLen=10 onkeydown="return
    fun_pTextarea_MaxLen(this);"></textarea>
    </BODY>
    </HTML>

    (phoenix398017)
    "hardcoded" <> wrote in message news:<#>...
    > I have a textbox with TextMode set to MultiLine. I also have the MaxLength
    > set to 255. This maxlength value seems to get ignored as the user can enter
    > unlimited characters. Does the multiline textbox not support maxlength?
    > How can I make the work?
    >
    > TIA
    i-Safire, May 25, 2004
    #5
  6. hardcoded

    cnicol

    Joined:
    Sep 26, 2007
    Messages:
    1
    Wow that's a really complex solution ... for a more lightweight solution perhaps this might fit some peoples needs:

    javascript:

    function textMaxLength(obj, maxLength, evt)
    {
    var charCode=(evt.which) ? evt.which : event.keyCode
    var max = maxLength - 0;
    var text = obj.value;
    if(text.length > max)
    {
    var ignoreKeys = [8,46,37,38,39,40,35,36];
    for(i=0;i<ignoreKeys.length;i++)
    {
    if(charCode==ignoreKeys)
    {
    return true;
    }
    }
    return false;
    }else
    {
    return true;
    }
    }

    for the html part :

    onKeyPress="return textMaxLength(this, '40', event);"
    cnicol, Sep 26, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    2
    Views:
    931
    Karl Seguin
    Feb 15, 2005
  2. Nathan Sokalski
    Replies:
    3
    Views:
    64,868
    zed_rhodes
    May 29, 2009
  3. danthman
    Replies:
    11
    Views:
    10,113
    elizas
    May 11, 2010
  4. Nathan Sokalski

    TextBox's MaxLength does not work when TextMode=MultiLine

    Nathan Sokalski, Sep 4, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    160
    S. Justin Gengo
    Sep 4, 2005
  5. dale zhang
    Replies:
    8
    Views:
    403
    Tintin
    Nov 30, 2004
Loading...

Share This Page