});
$("#<%=form1.ClientID %>").submit(function () {
var pas1 = $("#<%=txtPassWord.ClientID %>").val();
var pas2 = $("#<%=txtPassWord_Second.ClientID %>").val();
if (pas1 != pas2) {
alert("两次输入密码不一致,请重新输入");
return;
}
var sEmail = $("#<%=txtEmail.ClientID %>").val();
var check = /.+@.+/;
if (!check.test(sEmail)) {
alert("邮箱格式不正确,请重新输入");
return;
}
});
});</script>
</head>
</html>
Register.apsx.cs
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 登陆注册.DAL.DataSet1TableAdapters;
using System.Text.RegularExpressions;
namespace 登陆注册
{
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRegister_Click(object sender, EventArgs e)
{
//服务器版测试是否符合要求:邮箱饿坏密码
if (txtPassWord.Text != txtPassWord_Second.Text)
{
LabelErrorMsg.Text = "密码不一致!";
LabelErrorMsg.Visible = true;
return;
}
Regex reEmail = new Regex(@".+@.+");
if (!reEmail.IsMatch(txtEmail.Text))
{
LabelErrorMsg.Text = "Email格式不正确!";
LabelErrorMsg.Visible = true;
return;
}
LabelErrorMsg.Visible = false;
T_userInfoTableAdapter adapter = new T_userInfoTableAdapter();
if (adapter.GetDataByUserName(txtUserName.Text).Count >= 1)
{
errorMsg.Visible = true;
errorMsg.Text = "用户名重复,请重新输入!";
return;
}
long userId=Convert.ToInt64(adapter.InsertUser(txtUserName.Text, txtPassWord.Text,
txtEmail.Text));
//读取Session登陆信息
Session["登陆ID"] = userId;
Session["登陆标记"] = true;
//设置可以传参数的公共跳转页面
//作用1:告诉用户注册成功,作用2:防止用户点击刷新重复提交数据
Response.Redirect("target.aspx?Msg=注册成功即将转向登陆界面&RedirectURL=login.aspx");
}
protected void txtUserName_TextChanged(object sender, EventArgs e)
{
T_userInfoTableAdapter adapter = new T_userInfoTableAdapter();
if (adapter.GetDataByUserName(txtUserName.Text).Count >= 1) {
errorMsg.Visible = true;
errorMsg.Text = "用户名重复,请重新输入!";
}
else{
errorMsg.Visible = false;
}
}
}
}
其中要用到的正则表达式:
插入数据记录:INSERT INTO [dbo].[T_userInfo] ([sUserName], [sPassWord], [Email]) output Inserted.Id VALUES (@sUserName, @sPassWord, @Email)
(1、增加SQL语句,
(2、增加一个output Insert.Id 在Insert语句当中
(3、设置ExecuteMode属性为Scalar
7、三种控件的区别:html控件、服务端控件。runat=server 的控件(最好)
a1.Attributes("aaa")="建属性赋值";
8、验证控件
(1、 RequiredFieldVal:
设置哪个控件和显示名称(还有CausesValdation焦点移开) -----为空
初始值:InitleValue(请输入关键字、包括下拉框的初始值)