[英]RegularExpressionValidator does not work in asp.net
我想在asp.net中使用多行設置文本框的MaxLength屬性(例如100個字符)。 我不想使用JavaScript解決方案。 我用過 :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" SetFocusOnError="true" ValidationExpression="^[a-zA-Z.]{0,100}$"></asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="109px" Width="274px" ></asp:TextBox>
</div>
</form>
</body>
</html>
和這個 :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RegularExpressionValidator ID="rgConclusionValidator2" ControlToValidate="TextBox1" ValidationExpression="^[\s\S]{0,100}$" runat="server" SetFocusOnError="true" /></asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="109px" Width="274px" ></asp:TextBox>
</div>
</form>
</body>
</html>
但它們都不起作用。 我測試了最新的ie,firefox,chrome,opera。 非常感謝。
將此JavaScript函數用於maxlength並在OnKeypress上調用它。
function checktextarea(textvalue,message,limit)
{
var str="";
var str=textvalue.value;
if((str.length > limit ))
{
alert('Error : '+message + limit + ' characters\n'+ ' You entered text with Length '+str.length);
textvalue.focus()
return false;
}
else
{
return true;
}
}
這是工作,但是:
maxlength
屬性不同,它不允許您輸入超過100個字符。 您需要一些操作才能開始驗證。 在大多數情況下它是按鈕,所以只需添加它:
<asp:Button runat="server" Text="OK" />
您需要錯誤消息,如果驗證失敗將顯示給用戶 - 將ErrorMessage
屬性添加到驗證器:
<asp:RegularExpressionValidator ... ErrorMessage="ERROR!" />
就是這樣 - 鍵入文本,單擊按鈕,查看錯誤。
如果你想要動態限制文本長度 - 那么你需要JavaScript,你可以看到這個問題的樣本指定多行文本框的maxlength
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.