markzzz
markzzz

Reputation: 47945

asp:TextBox and MaxLength

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?

Upvotes: 2

Views: 3550

Answers (3)

nerochco
nerochco

Reputation: 21

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 />

Upvotes: 1

CodingIntrigue
CodingIntrigue

Reputation: 78535

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

<textarea id="txtSMSMessaggio" class="inputForm" maxlength="240" runat="server"></textarea>

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

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>

Upvotes: 0

Related Questions