【注册验证】 控制器(添加数据+后台验证)
[HttpGet] public ActionResult Add(string user_name = "", string user_id = "", string token = "") { ViewData["user_name"] = user_name; ViewData["user_id"] = user_id; ViewData["token"] = token; return View(); } [HttpPost] public ActionResult Add(UserInfo model, UserPersonal model2, string user_name = "", string user_id = "", string token = "", string FPhone = "", string FMail = "", string FSafeQuestion = "", string FSafeAnswer = "") { //=======更为为后台验证session形式验证 验证码 --- 0=======> ViewData["user_name"] = user_name; ViewData["user_id"] = user_id; ViewData["token"] = token; ViewData["FPhone"] = FPhone; ViewData["FMail"] = FMail; ViewData["FSafeAnswer"] = FSafeAnswer; ViewData["FSafeQuestion"] = FSafeQuestion; if (Session["checkcode"] != null && model.FVerifyImg.Trim() == Session["checkcode"].ToString()) { UserInfoBLL userinfobll = new UserInfoBLL(); UserPersonalBLL userpersonalbll = new UserPersonalBLL(); UserInfo userinfo = userinfobll.FindALL().Where<UserInfo>(s => s.FName == model.FName.Trim()).FirstOrDefault(); UserPersonal userpersonal = userpersonalbll.FindALL().Where<UserPersonal>(s => s.FId == model2.FId.Trim()).FirstOrDefault(); if (userinfo == null && userpersonal == null) { if (UserInfoBLL.Two_yanzheng(FPhone) == "0" && UserInfoBLL.Two_yanzheng(FMail) == "0") { model.FId = TableUtils.SetFID(); model.FPassWord = EncryptUtil.MD5String(model.FPassWord.Trim()).Trim(); //对密码进行加密 // model.FPassWord = model.FPassWord; model.FNoWorryId = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); model.FCreatorId = "-1";//自注册用户 model.FIsDelete = false;//表示未被删除 model.FCreateTime = DateTime.Now; model.FUpdatorId = "-1";//自注册用户 model.FUpdateTime = DateTime.Now; model.FInfoIsFinished = "0";//0资料未完善,1资料完善未审核,2审核通过,3审核未通过 model.FState = 0; model.FIsAdmin = 0; model2.FId = model.FId; if (userinfobll.Create(model)) { Session["TypePhone"] = FPhone; Session["TypeMail"] = FMail; bool result = userpersonalbll.Create(model2); } Session["Userid"] = model.FId; return RedirectToAction("Success/" + model.FNoWorryId + ""); } else { UserPersonal user = userpersonalbll.FindALL().Where(x => x.FPhone == model2.FPhone).FirstOrDefault(); UserPersonal user1 = userpersonalbll.FindALL().Where(x => x.FMail == model2.FMail).FirstOrDefault(); if (user != null) { ViewBag.ErrorMsg1 = "该手机号已被注册使用!请更换注册手机号!"; } if (user1 != null) { ViewBag.ErrorMsg1 = "该邮箱号已被注册使用!请更换注册邮箱号!"; } // ViewBag.ErrorMsg2 = "该邮箱号已被注册使用!"; return View(); } } else { //ViewBag.ErrorMsg = "该用户已存在!"; // ViewBag.ErrorMsg2 = "该用户已存在!"; return View(model2); } } else { ViewBag.ErrorMsg = "验证码不正确!"; return View(model); } }
®前台页面调用:
@* *************************************************************** * 公司名称 :XXXXXX * 系统名称 :湖北省XXXXXX公共服务平台 * 类 名 称 : Add.cshtml * 功能描述 :用户注册信息 * 业务描述 : * 作 者 名 :W T H * 开发日期 :2013-7-02 08:42:24 *************************************************************** * 修改履历 : * 修改编号 : *-------------------------------------------------------------- * 修改日期 :YYYY/MM/DD * 修 改 者 :XXXXXX * 修改内容 : *************************************************************** *@ @model UserInfo @{ ViewBag.Title = "用户注册"; // Layout = "~/Areas/PersonalSpace/Views/Shared/master/_LayoutPersonalHome.cshtml"; } <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 TrFSaveAnsweritional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trFSaveAnsweritional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>注册表单验证提示</title> <meta name="viewport" content="width=device-width" /> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> @Scripts.Render("~/Farm/jquery") @Scripts.Render("~/Farm/jqueryval") <!-- --> <script src="/js/jquery-1.7.1.js"></script> <script src="/res/dialog/lhgdialog.js"></script> <script src="/js/jquery.unobtrusive-ajax.js"></script> <script src="/js/jquery.validate.js"></script> <script src="/js/jquery.validate.unobtrusive.js"></script> <script src="/js/spaceenterconmmon.js"></script> @* <link href="/css/init.css" rel="stylesheet" />*@ <link href="/css/css/head_style.css" rel="stylesheet" /> <link href="/css/css/copyright.css" rel="stylesheet" /> <link href="/css/css/tree_style.css" rel="stylesheet" /> <link href="/css/css/style.css" rel="stylesheet" /> <link href="/css/page.css" rel="stylesheet" /> <style type="text/css"> .personal_banner { background-image: url(/images/login_banner.jpg); background-repeat: no-repeat; width: 964px; height: 80px; margin: 0 auto; } #weather { float: right; padding-right: 15px; padding-top: 15px; } .weather_font { color: #4d6066; font-size: 22px; font-weight: 300; } .weather_bottom_font { color: #178202; font-size: 14px; padding-left: 10px; } .ns51cn { padding-top : 50px; padding-left: 120px; } .mid { float: left; padding-left:10px; width: 735px; height: 400px; } .mid1 { float: left; width: 110px; height: 500px; } .mid2 { float: left; width: 120px; height: 400px; background-color:Black; } .context { padding-left: 150px; padding-top: 20px; } .context1 { padding-left: 160px; padding-top: 20px; } .context2 { padding-left: 140px; padding-top: 20px; } .context3 { padding-left: 0px; padding-top: 20px; } .context4 { padding-left: 153px; padding-top: 20px; } .context5 { padding-left: 180px; padding-top: 20px; } body { font-size: 200px; } .Fcenter { padding-left: 250px; padding-top: 20px; float: none; } .Copy { padding-left: 190px; padding-top: 20px; float: left; } body { color: #666666; font-size: 12px; } h1 { font-size: 20px; } input { border: #999999 1px solid; background-color: #FFFFEE; } .button2 { background-color: #FF9900; border-bottom: #E34A00 2px solid; border-right: #E34A00 2px solid; border-top: #FFB693 2px solid; border-left: #FFB693 2px solid; color: #FFFFFF; font-weight: bold; } dl { font: normal 12px/15px Arial; position: relative; width: 350px; } dt { clear: both; float: left; width: 80px; padding: 4px 0 2px 0; text-align: left; } dd { float: left; width: 200px; margin: 0 0 8px 0; padding-left: 6px; } .hint { display: none; position: absolute; right: 10px; width: 200px; margin-top: -4px; border: 1px solid #c93; padding: 10px 12px; background: #ffc url(/jscss/demoimg/200911/pointer.gif) no-repeat -10px 5px; } .hint .hint-pointer { position: absolute; left: -10px; top: 5px; width: 10px; height: 19px; background: url(/jscss/demoimg/200911/pointer.gif) left top no-repeat; } #login_head { background-repeat: repeat-x; color: #194C01; font-size: 14px; height: 36px; } #login_head_b { font-size: 14px; height: 36px; line-height: 36px; margin: 0 auto; width: 980px; } #head_nav { float: left; height: 36px; width: 450px; } #hd_r { float: right; font-size: 12px; margin-right: 18px; padding-top: 0; } #hd_r a { color: #000000; font-size: 12px; padding-top: 0; } #head_nav ul { list-style: none outside none; padding-left: 15px; margin: 0; } #head_nav ul li { color: #194C01; float: left; font-size: 14px; padding-left: 0; width: 80px; } #head_nav ul li a:link { background-repeat: no-repeat; color: #194C01; display: block; font-size: 14px; height: 36px; line-height: 36px; text-align: center; text-decoration: none; } .register_input_bg { background-image: url(images/register_input_bg.gif); background-repeat: repeat-x; font-size: 12px; border: 1px solid #94b4a1; height: 26px; line-height: 26px; width: 260px; color: #316603; } .register_input_bg1 { background-image: url(images/register_input_bg.gif); background-repeat: repeat-x; font-size: 12px; border: 1px solid #94b4a1; height: 26px; line-height: 26px; width: 100px; color: #316603; } .zhuce { background-image: url(images/zhuce.jpg); background-repeat: repeat-x; width: 90px; height: 25px; background-color: #CCC; font-size: 14px; } .b1, .b2, .b3, .b4, .b1b, .b2b, .b3b, .b4b, .b { display: block; overflow: hidden; } .b1, .b2, .b3, .b1b, .b2b, .b3b { height: 1px; } .b2, .b3, .b4, .b2b, .b3b, .b4b, .b { border-left: 1px solid #999; border-right: 1px solid #999; width: auto; } .b1, .b1b { margin: 0 5px; background: #999; width: auto; } .b2, .b2b { margin: 0 3px; border-width: 2px; } .b3, .b3b { margin: 0 2px; } .b4, .b4b { height: 2px; margin: 0 1px; } .d1 { background: null; } .k { width: 735px; height: 400px; } .wd { width: 80px; height: 23px; } .wd2 { float: left; width: 260px; height: 23px; } .tbclass { font-size: 10px; } .grey_font_style1 { color: #929291; font-size: 12px; display: none; } .grey_font_style2 { color: #929291; font-size: 14px; padding-top: 15px; } .block2222 { color: #929291; font-size: 12px; display: block; } </style> <link href="/css/register.css" rel="stylesheet" type="text/css" /> <script src="../../../../js/jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("form").submit(function () { if ($("#checkbox_xy").attr("checked") == null) { alert("请先阅读并接受服务条款!"); return false; } }); $('#submit').click(); // function myfunction() { // document.form1.FPhone.value = "";//实现将手机输入框提交后清空操作! // } }); </script> <script type="text/javascript"> $(function () { //加载完成后,更新天气 //请求天气数据 $.getJSON('/Home/Home/ReturnJsonWeather/101200101', null, function (data) { var d = data.weatherinfo; if (d != null) { $('#weatherDate').text(d['date_y']); //日期 $('#weatherWeek').text(d['week']); //星期 //今日天气 $('#weather_one_img').attr('src', '/images/WeatherPng/' + d['img1'] + '.png'); //天气图片 $('#weather_one_txt').text(d['weather1']); //天气信息 $('#weather_one_d').text(d['temp1']); //温度 } }); }); </script> <!--安全问题js--> <script type="text/javascript"> var arr2 = new Array(4); arr2["手机号码"] = new Array("请输入手机号码"); arr2["电子邮箱"] = new Array("请输入电子邮箱"); arr2["安全问题"] = new Array("请选择 ...", "我的小学名字?", "我妈妈的名字?", "我最难忘的日子?", "我的学号或(工号)是?", "我配偶的姓名是?", "我最喜欢的休闲运动是?", "我最喜欢的歌曲?", "我最喜爱的电影?", "自己编写问题?"); function removeinfo(classMenu) { for (var i = 0; i < classMenu.options.length; i++) { classMenu.options[i] = null; } } function changeMenu(classList, classMenu) { removeinfo(classMenu) for (var i = 0; i < classList.length; i++) { classMenu[i] = new Option(classList[i], classList[i]); } } </script> <script src="/js/jquery-1.7.1.min.js" type="text/javascript"></script> <script language="javascript" type="text/javascript"> function chg() { if ($("#sel").val() == "FPhone") { // $("#FPhone").focus(function () { // $("#FPhone").val(""); // }); // $("#FPhone").blur(function () { // $("#FPhone").val("请输入手机号码"); // }); $("#FPhone").show(); $("#FMail").hide(); // var o = document.form1.sel; // $("#FMail").val() = ""; $("#FSafeQuestion").hide(); // $("#FSafeQuestion").val() = ""; $("#key").hide(); // $("#key").val() = ""; } else if ($("#sel").val() == "FMail") { // $("#FMail").focus(function () { // $("#FMail").val(""); // }); // $("#FMail").blur(function () { // $("#FMail").val("请输入电子邮箱"); //请输入电子邮箱 // }); $("#FMail").show(); $("#FPhone").hide(); // $("#FPhone").val() = null; $("#FSafeQuestion").hide(); // $("#FSafeQuestion").val() = null; $("#key").hide(); // $("#key").val() = null; } else { $("#FSafeQuestion").show(); $("#key").show(); $("#FPhone").hide(); // $("#FPhone").val() = null; $("#FMail").hide(); // $("#FMail").val() = null; } } // $(function () { // $("#submit").click(function () { // if ("@ViewBag.ErrorMsg1" != "") { // $('#trErrorMsg').hide(); // alert("'@ViewBag.ErrorMsg1'"); // } //错误提示放哪儿? 阻挡不住验证的效果! // else if ("@ViewBag.ErrorMsg2" != "") { // alert("'@ViewBag.ErrorMsg2'"); // } // else { // // alert("该手机号码已存在!"); // return alert("该手机号码已存在!"); // // } // // return true; // }); // }); // function check() { // // // alert("'@ViewBag.ErrorMsg'"); // } </script> @* <script language="javascript" type="text/javascript"> function myfunction() { document.form1.FPhone.value = ""; } </script>*@ <meta name="viewport" content="width=device-width" /> </head> <body> <!-- 头部head --> <div id="login_head"> <div id="login_head_b"> <div id="head_nav"> </div> <div id="hd_r"> <a href="/Personal/Space/Login/Login">[登录]</a> <a href="/Personal/Space/UserInfo/Add"> [注册]</a> </div> </div> </div> <div class="two_bg"> <div id="content"> <div class="personal_banner"> <div id="weather"> <span class="weather_font" id="weather_one_d">00-00℃</span> <span id="weather_one_txt"> 加载中...</span><img id="weather_one_img" src="" style="width: 35px; margin-left: 10px" /> <br /> <img width="27" height="15" src="/images/today_icon.png" /> <span class="weather_bottom_font"><span id="weatherDate">2013年1月1号</span> <span id="weatherWeek">星期一</span></span> </div> </div> <div class="clear"> </div> <!-----------------------------------------------------------------------------------------------------------------------------------> <div class="ns51cn"> <!--ns51图片div--> <table> <tr> <td> <img src="~/images/img/ns51cn.jpg" alt="" /> </td> <td> <span style="font-size: 26px">注册</span> </td> </tr> </table> </div> <div class="mid1"> <!--左半部分空白填充区--> </div> <div class="mid"> <!--居中区 (圆形div边框)--> <b class="b1"></b><b class="FSaveAnswer d1"></b><b class="b3 d1"></b><b class="b4 d1"> </b> <div class="b d1 k"> @using (Html.BeginForm()) { <form action="" method="post" name="form1" target="_blank"> <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tbclass"> <tr> <td> <table width="960" height="649" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="574" valign="top" class=""> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> </td> </tr> </table> <table width="85%" height="520" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="right"> </td> <td colspan="3" align="left" style="color: Red;"> @* @ViewBag.ErrorMsg*@ @*=========信息开始==========*@ @if (!string.IsNullOrEmpty(ViewData["user_id"].ToString())) { <input type="hidden" name="FAlipayID" value="@(ViewData["user_id"].ToString())" /> } @if (!string.IsNullOrEmpty(ViewData["user_name"].ToString())) { <input type="hidden" name="FAlipayName" value="@(ViewData["user_name"].ToString())" /> } @if (!string.IsNullOrEmpty(ViewData["token"].ToString())) { <input type="hidden" name="FAlipayToken" value="@(ViewData["token"].ToString())" /> } @*=========信息结束==========*@ </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FName): </td> <td> @Html.TextBoxFor(m => Model.FName, new { @class = "register_input_bg", @maxlength = "16" }) </td> <td width="55%" class="grey_font_style">@Html.ValidationMessageFor(m => Model.FName) </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FPassWord): </td> <td> @Html.PasswordFor(m => Model.FPassWord, new { @class = "register_input_bg", @maxlength = "20" }) </td> <td class="grey_font_style">@Html.ValidationMessageFor(m => Model.FPassWord) </td> </tr> <tr> <td align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.ConfirmPassword): </td> <td> @Html.PasswordFor(m => Model.ConfirmPassword, new { @class = "register_input_bg", @maxlength = "20" }) </td> <td class="grey_font_style">@Html.ValidationMessageFor(m => Model.ConfirmPassword) </td> </tr> <tr> <!--找回密码的方式--> <tr> <td height="22" align="right" class="grey_font_style2"> <select class="wd" id="sel" onchange="chg()" size="1" style="font-family: Verdana,Arial; font-size: 10pt;"> <option value="FPhone">电话号码</option> <option value="FMail">电子邮箱</option> <option value="FSafeQuestion">安全问题</option> </select> <td height="22" align="right" class="grey_font_style2" id="change"> <input type="text" value="" name="FPhone" id="FPhone" size="1" style="font-family: Verdana,Arial; font-size: 10pt font-color: #CCC" class="wd2" /> <input type="text" value="" name="FMail" id="FMail" style="display: none; font-family: Verdana,Arial; font-size: 10pt;" class="wd2" /> <select name="FSafeQuestion" id="FSafeQuestion" size="1" style="display: none; font-family: Verdana,Arial; font-size: 10pt;" class="wd2"> <option value="" selected="selected">请选择</option> <option value="我的小学名字">我的小学名字?</option> <option value="我妈妈的名字">我妈妈的名字?</option> <option value="我最难忘的日子">我最难忘的日子?</option> <option value="我的学号或(工号)是">我的学号或(工号)是?</option> <option value="我配偶的姓名是">我配偶的姓名是?</option> <option value="我最喜欢的休闲运动是">我最喜欢的休闲运动是?</option> <option value="我最喜欢的歌曲">我最喜欢的歌曲?</option> <option value="我最喜爱的电影">我最喜爱的电影?</option> <option value="自己编写问题">自己编写问题?</option> </select> </td> </td> </tr> <tr id="key" style="display: none;"> <td align="right"> <span class="register_mid_font_red"></span>你的答案是: </td> <td> <input type="text" value="" name="FSafeAnswer" id="FSafeAnswer" style="font-family: Verdana,Arial; font-size: 10pt;" class="wd2" /> </td> </tr> <!--验证码--> <tr> <td height="61" align="right"> <span class="register_mid_font_red"></span>@Html.LabelFor(m => Model.FVerifyImg): </td> <td> @Html.TextBoxFor(m => Model.FVerifyImg, new { @class = "register_input_bg1", @maxlength = "4" }) <img src="@Url.Action("VirifyImg")" id="verify_code" alt="看不清?点击换一个" onclick="this.src=this.src+'?'" style="cursor:pointer;" /> @Html.ValidationMessageFor(m => Model.FVerifyImg) </td> <td> </td> </tr> <tr> <td height="45"> </td> <td colspan="2"> <table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="4%"> <input type="checkbox" name="checkbox" id="checkbox_xy" checked="checked" /> <label for="checkbox"> </label> </td> <td width="96%"> <span class="grey_font_style"> <label for="checkbox"> <a href="UserGreement" target="_blank">我已阅读并同意相关服务条款</a> </label> </span> <label for="checkbox"> </label> </td> </tr> <!--注册按钮--> <tr> <td height="84" colspan="3" align="left"> <input type="submit" id="submit" value=" 立即注册 " class="zhuce "/> <span class="grey_font_style_phoneandmail" style="font-family: Verdana,Arial; font-size: 14pt;">@ViewBag.ErrorMsg1</span><!--手机号已被使用--> </td> </tr> </table> </td> </tr> <tr> <td height="123"> </td> <td colspan="2" valign="middle"> </td> </tr> </table> </td> </tr> <tr> <td height="14" valign="top"> <img src="/images/index/register_down.gif" alt="" width="960" height="14" /> </td> </tr> </table> </td> </tr> </table> </form> } </div> <b class="b4b d1"></b><b class="b3b d1"></b><b class="FSaveAnswerb d1"></b><b class="b1b"> </b> </div> <span style="float: right; width: 100px; height: 400px;"> <!--左半部分空白填充区--> </span> <div class="Copy"> Copyright © 2013-2013 NS51 All Rights Reserved 农事无忧 版权所有</div> <!-- 尾部 foot --> <div class="clear"> </div> <div class="copyright"> 技术支持:中科宇图天下科技有限公司</div> <div class="declare"> 本站作为一个公益性的服务性网站,受政府资助资金局限,我们不得不充分利用网络现有资源来实现我们的最初目标,由此可能对您的知识产权造成损害,对此我们深表歉意。如果您认为我们的行为超出了您的许可范围,请及时告知我们,我们会在第一时间进行相关处理。</div> </div> </div> </body> </html>
有些梦想是遥不可及的,除非你坚持!