2

I wrote this:

<asp:TextBox ID="txtSMSMessaggio" CssClass="inputForm" MaxLength="240" TextMode="MultiLine" runat="server"></asp:TextBox>

but on client side I can't see the attribute maxlength (which I need). Why? And how can I fix it?

Jacob
  • 77,566
  • 24
  • 149
  • 228
markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

1

Can you just use a <textarea> tag and add runat="server"?

<textarea id="txtSMSMessaggio" class="inputForm" maxlength="240" runat="server"></textarea>
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • No, because I can't use that textarea as controls in the .cs. Such as txtSMSMessaggio.Text – markzzz Sep 10 '13 at 15:00
  • 1
    Declaring a ` – CodingIntrigue Sep 10 '13 at 15:02
  • You need to use [txtSMSMessaggio.Value](http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltextarea.value.aspx) instead – CodingIntrigue Sep 10 '13 at 15:17
1

you might not be interested in counting the characters in the TextBox, but its helpful to let the end user know there is a limit before it is reached. you can do something like

<script language="javascript" type="text/javascript">
var Maxlength = 240;
function taLimit() {
 var srcObject = event.srcElement;
 if (srcObject.value.length == Maxlength * 1) return false;
}

function taCount(tagName) {
 var srcObject = event.srcElement;
 if (srcObject.value.length > Maxlength * 1) srcObject.value = srcObject.value.substring(0, Maxlength * 1);
 if (tagName) visCnt.innerText = Maxlength - srcObject.value.length;
}
</script>

<asp:TextBox ID="txtSMSMessaggio" CssClass="inputForm" TextMode ="MultiLine" runat="server" 
onkeypress="return taLimit()" onkeyup="return taCount(Counter)" onfocus="return
taCount(Counter)" ></asp:TextBox><br />
(<span id="Counter">240</span> characters left)<br />
nerochco
  • 21
  • 2
0

You can try a Regular Expression to restrict the user to 240 characters:-

<asp:RegularExpressionValidator runat="server" ID="txtSMSMessaggio"
ControlToValidate="txtInput"
ValidationExpression="^[\s\S]{0,240}$"
ErrorMessage="Some error message"
Display="Dynamic">*</asp:RegularExpressionValidator>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331