Enter the format of the verification text box (the maximum length is 20, and the maximum number of Chinese characters is 10)

Source: Internet
Author: User

Background code:

First declare a method to verify that the information in the input box meets the requirements

/// <Summary>
/// Format the string length to distinguish Chinese characters from letters. Two Chinese characters, one letter or Digit
/// </Summary>
/// <Param name = "str"> string to be formatted </param>
/// <Param name = "N"> returns the New String Length. </param>
/// <Returns> New String </returns>
Public String stringformat (string STR, int N)
{
String temp = string. empty;
Int T = 0;
Char [] q = Str. tochararray ();

If (system. Text. encoding. Default. getbytecount (STR) <= N) // If the length is less than the required length, the original string is returned.
{
For (INT I = 0; I <q. Length & T <n; I ++)
{
If (INT) Q [I]> = 0x4e00 & (INT) Q [I] <= 0x9fa5) // whether the Chinese character is used
{
Temp + = Q [I];
T + = 2;
}
Else
{
Temp + = q [I];
T + = 1;
}
}

If (t <= n)
{
Return temp;

}
Else
Return NULL;


}
Else
{

Return null;
}
}

 

In the input box, add:

Private void txbName_LostFocus (object sender, RoutedEventArgs e)
{
If (txbName. Text. Length> 0)
{
CheckTextBox chkTxb = new CheckTextBox ();
String str = chkTxb. stringFormat (txbName. Text, 20 );
If (str = null)
MessageBox. Show ("the length of the entered characters is incorrect! ");
Else
TxbName. Text = str;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.