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> ");
}
}