Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MultiLine textbox -want to limit to 26 characters

Posted on 2006-03-31
10
Medium Priority
?
1,024 Views
Last Modified: 2009-12-18
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
0
Comment
Question by:MikeMCSD
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 10

Assisted Solution

by:aki4u
aki4u earned 800 total points
ID: 16345893
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>
0
 
LVL 10

Expert Comment

by:aki4u
ID: 16345902
asp:TextBox ID="txtEng" TextMode="MultiLine" Rows="3" Width="245"
  Runat="server" onkeyup="checkLength(this,26)"></asp:TextBox>
0
 
LVL 10

Expert Comment

by:aki4u
ID: 16345941
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

by:MikeMCSD
ID: 16346301
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!
0
 
LVL 3

Accepted Solution

by:
ptleitch earned 1200 total points
ID: 16346653
<asp:RegularExpressionValidator ID="revExample" runat="server" ControlToValidate="txtExample"
    Display="Dynamic" ErrorMessage="Exceeded 26 Characters!" ValidationExpression="^[\w\W]{0,26}$">
</asp:RegularExpressionValidator>
0
 
LVL 3

Expert Comment

by:ptleitch
ID: 16347392
@MikeMCSD have you tried my solution, I am pretty sure it will solve your problems even when a user presses the enter key.
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 16357073
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 . .
0
 
LVL 3

Expert Comment

by:ptleitch
ID: 16360544
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.
0
 
LVL 6

Expert Comment

by:fizch
ID: 16478522
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
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 16600634
I'll have to come back to this later. thanks for your help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question