Asp.net (c #) code that limits user input of specified characters and numbers

Source: Internet
Author: User

Here is the code:
Only user names can be entered: the user name must start with 0 ~ 9. ~ Z or ~ Z!
Copy codeThe Code is as follows:
Protected void Button3_Click (object sender, EventArgs e)
{
Int error_count = 0; // used to identify the validity of a user name
String str = TextBox1.Text. Trim ();
If (str = string. Empty)
{
Response. Write ("the user name cannot be blank! ");
Return;
}
Str = str. Substring (0, 1); // the user name contains 1st characters
String strchar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, B, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z ";
String [] VcArray = strchar. Split (',');
For (int I = 0; I <VcArray. Length; I ++)
{
If (str! = VcArray [I])
{
Error_count ++;
}
Else
{
Error_count = 0; // if the user name is valid, initialize the variable error_count to 0,
Break;
}
}
If (error_count> 0) // if the variable error_count is greater than 0, the user name is invalid.
{
Response. Write ("<script> alert ('user name must start with 0 ~ 9. ~ Z or ~ Z! ') </Script> ");
}
Else
{
Response. Write ("<script> alert ('legal user name, available! ') </Script> ");
}
}

Related Article

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.