MultiLine textbox -want to limit to 26 characters

I have a MultiLine textbox in a DataGrid that I want to limit to 26 characters max.

The MaxLength="26" property does not work for MulitLine textboxes.
I'm trying to use a Validator but it is not working:

<asp:TemplateColumn>
<ItemTemplate>
...........

<asp:TextBox ID="txtEng" TextMode="MultiLine" Rows="3" Width="245"
  Runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="regex"
  ControlToValidate="txtEng"
  ValidationExpression="^{1,26}$"
  Text="Engravings can only be a maxiumum of 26 characters"
  Display="Dynamic"
  ErrorMessage="Engravings can only be a maxiumum of 26 characters"
  Runat="server" />

Any ideas on how I can limit the text to 26 characters max?  thanks
LVL 16
MikeMCSDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aki4uCommented:
Add this to your page:

            <script language="javascript">
                  function checkLength(val,maxlen)
                  {
                        if (val.value.length > maxlen)
                              val.value = val.value.substr(0, maxlen)
                  }
            </script>


add this to your textbox:
onkeyup="checkLength(this,5)"

so it looks like this:
asp:TextBox ID="txtEng" TextMode="MultiLine" Rows="3" Width="245"
  Runat="server" onkeyup="checkLength(this,5)"></asp:TextBox>
aki4uCommented:
asp:TextBox ID="txtEng" TextMode="MultiLine" Rows="3" Width="245"
  Runat="server" onkeyup="checkLength(this,26)"></asp:TextBox>
aki4uCommented:
or you could use all in one line...like this:

onkeyup ="if(this.value.length>26) this.value=this.value.substring(0,26)"

you can also use "onblur" event
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

MikeMCSDAuthor Commented:
thanks ak . . that works expect for when
the enter key is pressed. example:
Dear John
blahab blah
....
then it limits the number of characters to less than 18 or so.

this multiline texbox is a pain!
ptleitchCommented:
<asp:RegularExpressionValidator ID="revExample" runat="server" ControlToValidate="txtExample"
    Display="Dynamic" ErrorMessage="Exceeded 26 Characters!" ValidationExpression="^[\w\W]{0,26}$">
</asp:RegularExpressionValidator>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ptleitchCommented:
@MikeMCSD have you tried my solution, I am pretty sure it will solve your problems even when a user presses the enter key.
MikeMCSDAuthor Commented:
thanks, just tried it pt . . if I type in:
Dear John
Thanks
Love Mary
...which is 24 characters, it gives the message
"Exceeded 26 Characters!"
I think maybe it's including the enter key as a character . .
ptleitchCommented:
I was going to mention that it probably counts the enter key as one of the characters.  This, however, is probably the closest you are going to get witht he Regular Expression Validator.  The enter key has to count as something.
fizchCommented:
Instead of using a regular expression validator, use a custom validator that checks the length of the text.

Private Sub cvEng_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvEng.ServerValidate
   if (txtEng.text.length > 26)
     args.IsValid = false;
   else
     args.IsValid = true;
   end if
End Sub
MikeMCSDAuthor Commented:
I'll have to come back to this later. thanks for your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.