The most typical method to control the number of characters! (Improved Version)
The most typical method to control the number of characters
When developing ASP. NET, we often encounter a headache: control the number of characters
Because the field length of the database is fixed, the most important thing during character input is to control the number of characters cannot exceed the length of the field. Otherwise, exceptions may lead to madness.
For single-line text boxes, we often use maxlength to control whether it is HTML control or web control, but this control cannot control Chinese characters, that is, the control of maxlength = 50, you can enter 50 English letters and 50 Chinese characters. In this case, the number of Chinese characters may overflow.
For multi-line text boxes, it is even worse. Using maxlenth does not work at all.
Advantages of the solution provided below:
1) The best way is to control the maximum number of words in the text box. If the number exceeds the limit, no input is allowed;
2) You should also be compatible with the pasting situation, and the previous methods on the Internet cannot control the paste situation;
3) For The Last Chinese character, you would rather discard it than enter one more character, for example, 50 characters. If you enter a Chinese character at 49th times, the final word count is 51, in this case, we recommend that you do not enter Chinese characters and ensure that the final number of characters is <= 50, because it is better to discard a Chinese character than to report an error to the database.
4) to improve development efficiency, minimizeCodeThis solution does not need to add any events to the text box, but is handled by the script block. Therefore, you only need to include the script block to the page, this should be quite convenient.
Improved Version modification: some input methods in the previous scheme cannot activate the onpress event, which makes Chinese unsupported. Now onkeyup event processing is used and the processing method is also modified, the original onpress event is for input control, while onkeyup can only judge the number of words that have already been input, and truncate for a long time.
In the original solution, a friend pointed out that the N (type) system does not report errors. Yes, if n (type) is used, it is character-based, you can ignore the possibility of database errors, but there is also a problem with the interface display, such as the address value on the interface, you want to be 100 characters, if you do not distinguish between Chinese and English words, up to 100 Chinese characters can be entered, which occupies 200 bytes of space. As a result, the adress will exceed the expected space during display, making the interface difficult to control. Therefore, this solution still has merits.
Specific new solution implementation:
1) include the following code to 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.doc umentelement. attachevent ('onkeyup', presslength );
Document.doc umentelement. attachevent ('onpaste ', limitlength );
// -->
</ Script >
2) add length = "N" (N indicates the number of words to be controlled) to the control, for example:
< Input Type = "Text" Length = "3" > < Textarea Length = "20" Rows = "2" Cols = "20" > </ Textarea > < ASP: textbox ID = "Textbox1" Runat = "Server" Length = "7" > </ ASP: textbox > < ASP: textbox ID = "Textbox2" Runat = "Server" Textmode = "Multiline" Length = "10" > </ ASP: textbox >
The preceding example shows the HTML control and web control. You only need to add a length.
Okay, you can try it out Oh: http://files.cnblogs.com/tintown/stringLenth3.rar (has been modified again "no length cannot be entered ")
Personal suggestion: 1) Place the script block in the JS file and reference it.
2) You can place the script in the basepage so that each page can be used.
3) You can use these script blocks to develop special server-side controls. I don't have time to develop them. If you have any friends, you can develop and release them!