The code is as follows: <asp:textbox id= "TextBox1" runat= "Server" Textmode=multiline height= "96px" width= "131px" maxlength=66></asp :textbox> You cannot control the length of the input after running, and you can enter it indefinitely. Why ... Hope Advice MaxLength for a single-line text box It's not going to work.
There seems to be no perfect solution involved in many aspects of text encoding such as in English, the number is not input but paste and so on.
A more feasible approach: Join the CustomValidator validation control and write your own script
To listen to Tang blog on this issue of the discussion: First.
<script language= "JavaScript" > <!-- String.prototype.len=function () { Return This.replace (/[^/x00-/xff]/g, "* *"). Length; } function checklength (source, arguments) { var validstrlength=50; if (arguments. Value.len () <=validstrlength) Arguments. IsValid = true; Else Arguments. IsValid = false; }
--> </script>
Use the above script in the interface, and then in the need to verify the place, plus CustomValidator validation control, the ClientValidationFunction attribute is specified as "Checklength", this method is the above client function, the function of the var validstrlength=50; Refers to the number of characters to validate. To illustrate, the number of characters here will automatically distinguish between Chinese characters, a Chinese character is automatically recorded as two characters, so you do not need to be like a single-line text box, set to half the total number of characters to control.
Well, with the above settings, you can see the effect of the control. Second. First, there's a place that doesn't feel good, that's it. Using the customer validator to control the MaxLength with the General TextBox settings is not very consistent. One is to restrict the input one is a hint error. I'm using JavaScript to control it. Set MaxLength for Multiline TextBox function Setmaxlength (object,length) { var result = true; var controlid = Document.selection.createRange (). parentelement (). ID; var controlvalue = Document.selection.createRange (). text; if (ControlID = = object.id && controlvalue!= "") { result = true; } else if (object.value.length >= length) { result = false; } if (window.event) { Window.event.returnValue = result; return result; } }
Check MaxLength for multiline TextBox when paste function Limitpaste (object,length) { var templength = 0; if (document.selection) { if (Document.selection.createRange (). parentelement (). id = = object.id) { Templength = Document.selection.createRange (). Text.length; } } var tempvalue = window.clipboardData.getData ("Text"); Templength = Object.value.length + tempvalue.length-templength; if (templength > Length) { templength = length; Tempvalue = Tempvalue.substr (0,tempvalue.length-templength); Window.clipboardData.setData ("Text", Tempvalue); }
Window.event.returnValue = true; } Then set 2 properties for a multiline TextBox. Onkeypress= "Javascript:setmaxlength (this,100);" Onpaste= "limitpaste (this, 100)" Third. The second option is good, but there is also the problem that there is no distinction between Chinese and English characters, the Chinese character should count to be two, for which I have extended: <script language=javascript> <!--
String.prototype.len=function () { Return This.replace (/[^/x00-/xff]/g, "* *"). Length; }
Set MaxLength for Multiline TextBox function Setmaxlength (object,length) { var result = true; var controlid = Document.selection.createRange (). parentelement (). ID; var controlvalue = Document.selection.createRange (). text; if (ControlID = = object.id && controlvalue!= "") { result = true; } else if (Object.value.len () >= length) { result = false; } if (window.event) { Window.event.returnValue = result; return result; } }
Check MaxLength for multiline TextBox when paste function Limitpaste (object,length) { var templength = 0; if (document.selection) { if (Document.selection.createRange (). parentelement (). id = = object.id) { Templength = Document.selection.createRange (). Text.len (); } } var tempvalue = window.clipboardData.getData ("Text"); Templength = Object.value.len () + Tempvalue.len ()-templength; if (templength > Length) { templength = length; alert (templength); alert (Tempvalue); var tt= ""; For (Var i=0;i<tempvalue.len ()-templength;i++) { if (Tt.len () < (Tempvalue.len ()-templength)) Tt=tempvalue.substr (0,i+1); Else Break } Tempvalue=tt; Window.clipboardData.setData ("Text", Tempvalue); }
Window.event.returnValue = true; }
--> </script>
Then set 2 properties for a multiline TextBox. Onkeypress= "Javascript:setmaxlength (this,100);" Onpaste= "limitpaste (this, 100)" Now, you can automatically distinguish between Chinese and English, this scheme is good source: http://hi.baidu.com/lisoaring/blog/item/a3deab189398e80734fa41c9.html |