用户注册模块详解(30)
由于工作需要,對(duì)方要求me做個(gè)模塊或者其他實(shí)體東西,于是就選擇了大家經(jīng)常用到的用戶注冊(cè)模塊,廢話不多說(shuō)先看效果圖,沒(méi)有經(jīng)過(guò)任何美工處理,效果一般,效果圖:
在設(shè)計(jì)注冊(cè)模塊之前需要先知道此模塊的工作流程,即各個(gè)控件的用途,比如使用驗(yàn)證控件,電話號(hào)碼的驗(yàn)證需要正則表達(dá)式,郵箱也是,主要還是控件與SQL的交互。
html代碼:
<head runat="server"><title>新用戶注冊(cè)</title>
<style type="text/css">
.style1 {
text-align: center;
width: 870px;
margin-left: 47px;
}
.style2
{
font-size: 8pt;
}
.style3
{
font-size: 5pt;
}
</style>
</head>
<body>
<form id="form1" runat="server" action="index.aspx">
<div class="style1">
用戶注冊(cè)
<hr style="width:50%; color:blue"/>
<table align="center">
<tr><td>
會(huì)員姓名:<asp:TextBox ID="txtName" runat="server"
ToolTip="字母、下劃線、數(shù)字"
ontextchanged="txtName_TextChanged"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" Display="Dynamic" ErrorMessage="會(huì)員姓名不能為空"></asp:RequiredFieldValidator>
</td>
<td> <asp:Label ID="lblMZ" runat="server"></asp:Label></td>
<td><asp:Label ID="lblZC" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
密碼:
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPwd" Display="Dynamic" ErrorMessage="密碼不能為空,建議大于6位數(shù)"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
確認(rèn)密碼:<asp:TextBox ID="txtPwdOk" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtPwd" ControlToValidate="txtPwdOk" Display="Dynamic"
ErrorMessage="密碼不相同,請(qǐng)重新輸入"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
昵稱:
<asp:TextBox ID="txtNick" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
性別: <asp:RadioButton ID="rdiobtnM" runat="server" GroupName="sex"
Text="男" />
<asp:RadioButton ID="rdobtnW" runat="server" GroupName="sex" Text="女" />
</td>
</tr>
<tr>
<td>
電話:
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtPhone" Display="Dynamic" ErrorMessage="請(qǐng)正確輸入手機(jī)或固話"
ValidationExpression="\d{11}|(\d{3,4}-)?\d{7,8}"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
郵箱:
<asp:TextBox ID="txtMail" runat="server"></asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtMail" ErrorMessage="請(qǐng)正確輸入郵箱地址"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
所在城市:<asp:TextBox ID="txtCity" runat="server"
></asp:TextBox>
</td>
</tr>
<tr>
<td style="font-size: 5pt">
<asp:Button ID="btnZhuCe" runat="server" Text="注冊(cè)" οnclick="btnZhuCe_Click" />
<asp:Button ID="btnQuXiao" runat="server" Text="取消" οnclick="btnQuXiao_Click" />
</td>
<td>
</td>
</tr>
<tr><td><span class="style2"><a href="index.aspx">所有已注冊(cè)用戶</a> </span>
<span class="style3"><a href="changeinfo.aspx" style="font-size: 8pt">修改個(gè)人信息</a></span></td></tr>
</table>
</div>
</form>
</body>
</html>
?
?
在用戶注冊(cè)頁(yè)面需要判斷用戶輸入的是否為空,還要判斷用戶時(shí)候已經(jīng)注冊(cè)過(guò),用戶的輸入的用戶名格式是否合法。該功能用TextChanged來(lái)實(shí)現(xiàn),調(diào)用isnamefomrmar方法判斷用戶輸入的用戶名是否正確,最后調(diào)用isname方法判斷用戶名是否已經(jīng)存在,并給出相應(yīng)的提示,實(shí)現(xiàn)的代碼如下:
protected void txtName_TextChanged(object sender, EventArgs e){
//判斷用戶名是否為空
if (txtName.Text == "?")
{
//給出提示信息
ClientScript.RegisterStartupScript(this.GetType(), "?", "<script>alert('用戶名不能為空!')</script>");
}
else
{
//判斷用戶名是否符合正則表達(dá)式
if (isNameFormar())
{
//判斷用戶是否已注冊(cè)
if (isName())
{
lblZC.Text = "?";
}
else
{
lblZC.Text = "用戶可以注冊(cè)";
}
}
else
{//用戶名不符合正則表達(dá)式,則把清空
txtName.Text = "?";
}
}
?
?
自定義方法isnameformar用來(lái)判斷用戶輸入的格式是否正確,會(huì)員輸入的格式是指用戶名只能包含數(shù)字,字母及下劃線,主要通過(guò)regex的ismatch方法實(shí)現(xiàn),看是否滿足正則表達(dá)式,然后返回布爾值,實(shí)現(xiàn)的代碼如下:
protected bool isNameFormar(){
//定義個(gè)布爾型的變量,初始為false
bool blNameFormar = false;
//設(shè)置正則表達(dá)式
Regex re = new Regex("^[a-zA-Z0-9_]{3,16}$");
//用IsMatch方法判斷用戶輸入信息是否合法
if (re.IsMatch(txtName.Text))
{
//設(shè)置布爾值為true
blNameFormar = true;
this.lblMZ.ForeColor = System.Drawing.Color.Black;
}
else
{
//設(shè)置布爾值為false
blNameFormar = false;
lblMZ.Text = "用戶格式不正確";
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用戶名格式不正確!')</script>");
lblMZ.ForeColor = System.Drawing.Color.Red;
}
//返回布爾值
return blNameFormar;
}
自定義方法isname來(lái)判斷用戶輸入的用戶名是否已經(jīng)存在,可通過(guò)SQL語(yǔ)句實(shí)現(xiàn),如果已存在返回布爾值true,否則返回false,實(shí)現(xiàn)代碼如下:
protected bool isName(){
//定義個(gè)布爾型的變量,初始為false
bool blisname = false;
//定義SQL語(yǔ)句,實(shí)現(xiàn)查詢功能
string sqlSel = "select count(*) from userInfo where userName='" + txtName.Text + "'";
//創(chuàng)建數(shù)據(jù)庫(kù)連接
SqlConnection con = new SqlConnection("server=.;database=login;uid=sa;pwd=szl;");
//創(chuàng)建SqlCommand對(duì)象com
SqlCommand com = new SqlCommand(sqlSel, con);
//打開數(shù)據(jù)庫(kù)連接
con.Open();
//判斷查詢結(jié)果中第一行的第一列是否存在
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
//存在,返回為true
blisname = true ;
}
else
{
//不存在,返回為false
blisname =false ;
}
//關(guān)閉數(shù)據(jù)庫(kù)連接
con.Close();
//返回布爾值
return blisname;
}
在“注冊(cè)”按鈕單擊事件中,先判斷用戶名是否已經(jīng)存在,和格式是否正確,在滿足這兩個(gè)條件的基礎(chǔ)上,在把用戶的信息添加到數(shù)據(jù)庫(kù)中,主要通過(guò)insert語(yǔ)句實(shí)現(xiàn),為了提高保密性,我們對(duì)密碼進(jìn)行了加密,使用MD5加密方式,代碼實(shí)現(xiàn)如下:
protected void btnZhuCe_Click(object sender, EventArgs e){
//調(diào)用isNameFormar自定義方法判斷用戶名輸入的是否滿足要求
if (isNameFormar())
{
//調(diào)用自定義isName方法判斷用戶名是否已存在
if (isName())
{
//用lable控件顯示提示信息
lblMZ.Text = "用戶已存在";
lblMZ.ForeColor = System.Drawing.Color.Red;//設(shè)置字體演示
}
else
{
//獲取用戶填寫的會(huì)員名
string userName = txtName.Text;
//獲取用戶填寫的密碼并進(jìn)行加密
string userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text, "MD5");
//獲取用戶填寫的昵稱名稱
string nickName = txtNick.Text;
//獲取用戶性別
string sex = "";
if (rdiobtnM.Text=="男")
{
sex = "男";
}
else
{
sex = "女";
}
//獲取用戶電話
string phone = txtPhone.Text;
//獲取用戶輸入城市名
string city = txtCity.Text;
//獲取用戶輸入的e_mail
string email = txtMail.Text;
//定義一個(gè)SQL語(yǔ)句,實(shí)現(xiàn)用戶信息的添加
string sqlIns = "insert into userInfo(userName,userPass,nickName,sex,phone,email,city) values ('" + userName + "','" + userPass + "','" + nickName + "','" + sex + "','" + phone + "','" + email + "','" + city + "')";
//創(chuàng)建數(shù)據(jù)庫(kù)連接
SqlConnection con = new SqlConnection("server=.;database=login;uid=sa;pwd=szl;");
//打開數(shù)據(jù)庫(kù)連接
con.Open();
//定義命令對(duì)象
SqlCommand com = new SqlCommand(sqlIns, con);
//判斷受影響的行數(shù),大于0,證明添加成功,反之不成功
if (com.ExecuteNonQuery()>0)
{
ClientScript.RegisterStartupScript(this.GetType(),"", "<script>alert('注冊(cè)成功!')</script>");
//清空文本框中的信息
txtName.Text = txtNick.Text = txtPhone.Text = txtMail.Text = txtCity.Text = "";
lblMZ.Text = "";
Response.Redirect("");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('請(qǐng)正確填寫信息!')</script>");
}
con.Close();
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('請(qǐng)正確填寫信息!')</script>");
}
}
主要還是對(duì)整個(gè)流程的把握,注意!
轉(zhuǎn)載于:https://www.cnblogs.com/shenzhoulong/archive/2010/06/19/1761038.html
總結(jié)
以上是生活随笔為你收集整理的用户注册模块详解(30)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据归一化处理方法_数据预处理:归一化和
- 下一篇: Oracle delete trunca