2016-05-14 61 views
0

我在asp.net 登記表我想打一個失敗的評論這是一個多行文本框,我想,讓使用只寫100個字符在此框中,如果他輸入更多..我想只接受前100個字符,並刪除其他然後用文字 的接受的一部分向用戶發送錯誤按摩我曾嘗試使用的RegularExpressionValidator這樣的:驗證工具

<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="TextBox1" ErrorMessage="Comment should be less the 100 letter." ForeColor="Red" ValidationExpression="^.{1,100}$"></asp:RegularExpressionValidator>

但我仍然想要顯示使用r中接受了一部分,並從盒子 刪除其餘的,所以我怎麼能做到這一點使用驗證工具

回答

1

只是你Page_Load事件

TextBox1.Attributes.Remove("MaxLength"); 
TextBox1.Attributes.Add("MaxLength", "100"); 

添加兩行,這將讓你即使設置它的工作TextBoxMultiLine模式。

使用JavaScript,你可以做這樣的

<script type="text/javascript"> 
     function LimtCharacters(txtMsg, CharLength, indicator) { 
      chars = txtMsg.value.length; 
      document.getElementById(indicator).innerHTML = CharLength - chars; 
      if (chars > CharLength) { 
       txtMsg.value = txtMsg.value.substring(0, CharLength); 
      } 
     } 
</script> 

寫您的aspx頁面

Number of Characters Left: 

<label id="lblcount" style="background-color:#E2EEF1;color:Red;font-weight:bold;">100</label><br/> 

<asp:TextBox ID="multiTxtBox" TextMode="MultiLine" onkeyup="LimtCharacters(this,100,'lblcount');" runat="server"></asp:TextBox> 

希望這將有助於!

+1

我從來沒有想到這一點!第二行是否會自動覆蓋/排除第一行? – IrishChieftain

+1

是的正確!你得到了點:) –

+0

這是很好的,但我要告訴他的文字是高大的用戶,並顯示接受文字 – Bayan