asp.net of c a code that restricts the user from entering specified characters and Numbers

  • 2020-05-10 17:59:21
  • OfStack

1 below is the code:
Only user names are allowed: the beginning of the user name must be 0~9, a~z, or A~Z!
 
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(" User name cannot be empty! "); 
return; 
} 
str = str.Substring(0, 1); // User name control 1 A character  
string strchar = "0,1,2,3,4,5,6,7,8,9,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, change the variable error_count Initialized to 0 .  
break; 
} 
} 
if (error_count > 0) // If the variable error_count Is greater than 0 , the user name is illegal.  
{ 
Response.Write("<script>alert('  The user name must begin with 0~9 , a~z or A~Z  ! ')</script>"); 
} 
else 
{ 
Response.Write("<script>alert('  Legitimate user name, can be used! ')</script>"); 
} 
} 

Related articles: