asp.net (C #) limits the user's code to enter specified characters and numbers

Source: Internet
Author: User
Tags empty net string variable
Here's the code:
Allow only user name input: User name must begin with 0~9, a~z or A~z!
Copy CodeThe code is as follows:
protected void Button3_Click (object sender, EventArgs e)
{
int error_count = 0; Legality used to identify the 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 1th 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, 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 illegal.
{
Response.Write (' <script>alert (' Beginning of user name, must be 0~9, a~z, or a~z! ') </script> ");
}
Else
{
Response.Write ("<script>alert" (' Legal user name, can be used!) ') </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.