When we develop in asp.net, we often encounter a headache problem: the control of character number
Because the database's field length is fixed, so in the character input, the most important thing is to control the number of characters can not exceed the length of the field, otherwise, an abnormal will make people crazy.
For Single-line text boxes, whether HTML or Web controls, we often use maxlength to control, but this control is not controlled to the Chinese character, that is, maxlength=50 control, you can enter 50 English and 50 Chinese, so, Or it can cause the overflow of the number of Chinese characters.
For multiline text boxes, that's even worse, and using maxlenth doesn't work at all.
The scenario benefits offered below are:
1 The best way is to enter the text box, control to the maximum number of words, more than can not be input;
2 for Sticky situation also to be compatible with the previous online methods can not control the situation of paste;
3 for the last one of the Chinese characters, would rather not be more than one character, such as 50 characters, in the 49th, the final input of a Chinese, will lead to a final word of 51, and this situation, the recommendation is not to enter Chinese, to ensure that the final number of characters <=50 best, Because it's better to give up a Chinese character than a database error.
4 in order to improve the efficiency of the development, as much as possible to reduce the amount of code, this scenario does not need to add any event for the text box, but by the script block to solve their own, so just want to include the script block drink included in the page, it should be quite convenient.
Improved version: As a result of the previous scheme, some input methods can not activate the Onpress event, resulting in the Chinese do not support, now using onkeyup Event processing, in the processing mode has also been modified, the original Onpress event is for input control, And onkeyup can only be entered after the word number of words to judge, too long to cut short treatment.
In the original scenario, a friend pointed out: the Data using N (type) system will not be an error, yes, if the use of n (type), is based on characters, you can not consider the possibility of database errors, but also there is an interface to display the problem, such as the interface of the address value, Hope is 100 characters, if not in Chinese and English word difference, will enter up to 100 Mandarin, thus accounting for 200 bytes of space, resulting in adress in the display will exceed the expected space, the interface is not easy to control. Therefore, there is merit in this scheme.
Specific new program implementation:
1 The following code is included in the page: < 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;
var tempstring = Object.value;
var tt = "";
for (var i = 0; I < Length;i + +)
{
if (Tt.len () < length)
tt = TEMPSTRING.SUBSTR (0, i + 1);
Else
break;
}
if (Tt.len () > Length)
tt = TT.SUBSTR (0, tt.length-1);
Object.value = TT;
}
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;
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;
}
if (Tt.len () <= 0)
{
Window.event.returnValue = false;
}
Else
{
Tempvalue = TT;
Window.clipboardData.setData ("Text", Tempvalue);
Window.event.returnValue = true;
}
}
}
function Presslength ()
{
if (Event.srcElement.type = = "Text" | | Event.srcElement.type = = "textarea")
{
if (event.srcelement.length!=null)
Setmaxlength (event.srcelement,event.srcelement.length);
}
}
function Limitlength ()
{
if (Event.srcElement.type = = "Text" | | Event.srcElement.type = = "textarea")
{
if (event.srcElement.length!= null)
Limitpaste (event.srcelement,event.srcelement.length);
}
}
Document.documentElement.attachEvent (' onkeyup ', presslength);
Document.documentElement.attachEvent (' Onpaste ', limitlength);
-->
</script >
2 Add Length= "n" (n is the number of words to be controlled) on controls that need to be controlled, such as:
< INPUT type = "text" length = "3" > < TEXTAREA length = "rows =" 2 "cols =" ></TEXTAREA > &L T Asp:textbox id = "TextBox1" runat = "server" length = "7" ></Asp:textbox > < Asp:textbox id = "TextBox2" ru NAT = "Server" TextMode = "MultiLine" length = "ten" ></Asp:textbox >
Above is an example of an HTML control and a Web control, just add a length to it.
All right, let's get a taste of it. Oh: http://www.cnblogs.com/Files/tintown/stringLen