我写了这个:
<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>
您可能对计算 TextBox 中的字符不感兴趣,但它有助于让最终用户知道在达到限制之前存在限制。你可以做类似的事情
<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>