asp:TextBox and MaxLength
本文关键字:MaxLength and TextBox asp | 更新日期: 2023-09-27 18:30:46
我写了这个:
<asp:TextBox ID="txtSMSMessaggio" CssClass="inputForm" MaxLength="240" TextMode="MultiLine" runat="server"></asp:TextBox>
但是在客户端,我看不到属性maxlength(我需要)。为什么?我该如何解决它?
您可以使用<textarea>
标签并添加runat="server"
吗?
<textarea id="txtSMSMessaggio" class="inputForm" maxlength="240" runat="server"></textarea>
您可能
对计算文本框中的字符不感兴趣,但在达到限制之前让最终用户知道存在限制会很有帮助。你可以做这样的事情
<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 />
您可以尝试使用正则表达式将用户限制为 240 个字符:-
<asp:RegularExpressionValidator runat="server" ID="txtSMSMessaggio"
ControlToValidate="txtInput"
ValidationExpression="^['s'S]{0,240}$"
ErrorMessage="Some error message"
Display="Dynamic">*</asp:RegularExpressionValidator>