Because of the work needs, the other side asked me to do a module or other entities, so the choice of users often use the user registration module, nonsense not to say the first look at the effect diagram, without any art processing, the effect of general, Effect diagram:
Before you design the registration module, you need to know the workflow of the module, that is, the purpose of each control, such as using validation controls, the validation of a phone number requires a regular expression, and a mailbox, primarily, or a control's interaction with SQL.
HTML code:
In the user registration page needs to determine whether the user input is empty, but also to determine the user has been registered, the user's input user name format is legitimate. This function is implemented with TextChanged, call the Isnamefomrmar method to determine whether the user entered the user name is correct, and finally call the Isname method to determine whether the user name already exists, and give the corresponding prompt, the implementation of the code is as follows:
protected void Txtname_textchanged (object sender, EventArgs e)
{
//determine if the user name is empty
if (txtName.Text = = "")
{
//give the prompt information
Clientscript.registerstartupscript (this. GetType (), "", "<script>alert (' User name cannot be empty. ') </script> ");
}
else
{
//determine if the user name conforms to the regular expression
if (Isnameformar ())
{
//Determines whether the user has registered
if (isname ())
{
Lblzc.text = "";
}
else
{
Lblzc.text = "user can register";
}
}
else
{//user name does not match regular expression, then empty
txtName.Text = "";
}
}
Custom method Isnameformar is used to determine whether the user input format is correct, the member input format refers to the user name can only contain numbers, letters and underscores, mainly through the IsMatch method of the regex implementation, to see if the regular expression is satisfied, and then return the Boolean value, the implementation of the code is as follows:
protected bool Isnameformar ()
{
//defines a variable of type Boolean, initially false
bool Blnameformar = false;
Set regular expression
regex re = new regex ("^[a-za-z0-9_]{3,16}$");
Use the IsMatch method to determine if the user's input information is legitimate
if (re). IsMatch (txtName.Text))
{
//sets a Boolean value of true
Blnameformar = true;
This.lblMZ.ForeColor = System.Drawing.Color.Black;
}
else
{
//sets a Boolean value of false
Blnameformar = false;
Lblmz.text = "user format is incorrect";
Clientscript.registerstartupscript (this. GetType (), "", "<script>alert (' User name format is incorrect. ') </script> ");
Lblmz.forecolor = System.Drawing.Color.Red;
}
Returns a Boolean value of
return Blnameformar;
}
The custom method Isname to determine if the user entered user name already exists, can be implemented by the SQL statement, if the return Boolean value is already present true, otherwise return false, the implementation code is as follows:
protected bool Isname ()
{
//defines a variable of type Boolean, initially false
bool Blisname = false;
Define the SQL statement that implements the query function
string sqlsel = "SELECT count (*) from UserInfo where username= '" + txtName.Text + "'";
Create a database connection
SqlConnection con = new SqlConnection ("server=.; Database=login;uid=sa;pwd=szl; ");
Create SqlCommand object com
SqlCommand com = new SqlCommand (Sqlsel, con);
Open database Connection
con. Open ();
Determines whether the first column of the first row in the query results exists if
(Convert.ToInt32 (COM). ExecuteScalar ()) > 0)
{
//presence, return true
Blisname = true;
}
else
{
//does not exist, returns false
Blisname =false;
}
Close database connection
con. Close ();
Returns a Boolean value of
return blisname;
}
In the "Register" button click event, the first to determine whether the user name is already present, and the format is correct, on the basis of satisfying these two conditions, the user's information is added to the database, mainly through the INSERT statement implementation, in order to improve confidentiality, we encrypt the password, using MD5 encryption method, The code is implemented as follows:
protected void Btnzhuce_click (object sender, EventArgs e) {//Call Isnameformar custom method to determine if the user name input satisfies the requirements
if (Isnameformar ()) {//Call custom Isname method to determine if the user name already exists if (Isname ())
{//Use lable control to display prompt information Lblmz.text = "User already exists";
Lblmz.forecolor = system.drawing.color.red;//Set Font demo} else
{//Get user filled in member name string userName = txtName.Text; Gets the password that the user fills in and encrypts string userpass = FormsAuthentication.HashPasswordForStoringInConfigFile (Txtpwd.text,
"MD5");
Gets the nickname name that the user fills in string nickname = Txtnick.text;
Get user sex string sex = "";
if (rdiobtnm.text== "male") {sex = "male";
} else {sex = "female";
}//Get user phone string phone = Txtphone.text;
Gets the user entered the city name of the string cities = Txtcity.text;
Gets the user input of the e_mail string email = txtmail.text; Define an SQL statement that implements the addition of the user information string sqlins = "INSERT INTO UserInfo (username,userpass,nickname,sex,phone,email , city) VALUES (' "+ UserName +" ', ' "+ Userpass +" ', ' "+ Nickname +" ', ' "+ Sex +" ', ' "+ Phone +" ', ' "+ email +" ', ' "+
City + "')"; Create a database connection SqlConnection con = new SqlConnection ("server=.;
Database=login;uid=sa;pwd=szl; "); Open database connection con.
Open ();
Defines the Command object SqlCommand com = new SqlCommand (Sqlins, con); Determine the number of rows affected, greater than 0, to prove that the addition succeeds, and vice versa if (COM) is unsuccessful. ExecuteNonQuery ()>0) {Clientscript.registerstartupscript (this. GetType (), "", "<script>alert (' registered successfully.
') </script> ");
Empty the information in the text box txtName.Text = Txtnick.text = Txtphone.text = Txtmail.text = Txtcity.text = "";
Lblmz.text = "";
Response.Redirect (""); } else {Clientscript.registerstartupscript (this. GetType (), "", "<script>alert (' please fill in the information correctly.
') </script> "); } con.
Close (); }} else {Clientscript.registerstartupscript (this. GetType (), "", "<script>alert (' please fill in the information correctly.
') </script> "); }
}