MVC注册验证:js方法(jQuery)

控制器端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication5注册验证.Models
{
    public class ZhuceBF
    {
        private mydboDataContext _Context = new mydboDataContext();
        public List<zhuce> Select()
        {
            return _Context.zhuce.ToList();
        }
        public zhuce Selectbyid(string id)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
        }



        public bool Selectid(string id)
        {
            var query = _Context.zhuce.Where(p => p.ID == id);
            if (query.Count() > 0)
            {
                return true;
                
            }
            return false;
        }


        public void Insert(string id,string username,string userpwd)
        {
            if (username == "" || username == " ")
            {
            }
            else
            {
                zhuce data = new zhuce();
                data.ID = id;
                data.Username = username;
                data.Userpwd = userpwd;
                _Context.zhuce.InsertOnSubmit(data);
                _Context.SubmitChanges();
            }
        }

        public void Update(string id, string username, string userpwd)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                zhuce data = query.First();
                data.ID = id;
                data.Username = username;
                data.Userpwd = userpwd;
            }
            _Context.SubmitChanges();
        }

        public void Delete(string id)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                zhuce data = query.First();
                _Context.zhuce.DeleteOnSubmit(data);
                _Context.SubmitChanges();
            }
        }
    }
}

视图端代码:

@using MvcApplication5注册验证.Models;
@using MvcApplication5注册验证.Controllers;

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
        <style type="text/css">
        .tr1
         {
            background-color:blue;
            text-align:center;
            font-weight:bold;
        }
        .tr2       
        {
            background-color:#FFFFCC;
            text-align:center;
        }
    </style>
</head>
<body>
    
    <div id="tishi"></div>
    <center><h2>注册新用户</h2></center>
   <center>
       
      <form action="/Home/Insert" method="post"> 
   <table  width="60%" cellpadding="5" cellspacing="1" border="0">      
        <tr class="tr1">
            <td>用户名ID:</td>
            <td>  <input type="text" name="id" id="UserId" >
            <input id="Button1" type="button" onclick="yanzheng()"   value="唯一验证" /><br/>
                <div id="me"></div>
            </td>
            </tr>
        <tr class ="tr2">
         
            <td>用户名:</td>
            <td><input id="Text2" type="text" name="username" /></td>
        </tr>
        <tr class="tr2">
            <td>密码</td>
            <td><input id="Text3" type="password" name="userpwd"/></td>
        </tr>
        <tr class ="tr2">
            <td>确认密码</td>
            <td><input id="Text4" type="password" /></td>
        </tr>
        <tr class="tr2">
            <td"><input id="Submit1" type="submit" value="注册" onclick="return checkuid()"/>
                <a href="/Home/chakan"><input id="Button2" type="button" value="查看" /></a>              
            </td>         
        </tr>             
    </table>
         <form/>
    </center>
   
</body>
</html>  
  <script type="text/javascript" src="js/JavaScript1.js"></script>
            <script>
                function checkuid()
                {
                    var a = document.getElementById("Text2");
                    var b = document.getElementById("tishi");
                    var c=document.getElementById("Text3");
                    var d=document.getElementById("Text4");
                    var txt1=c.value;
                    var txt2=d.value;
                    var x = a.value;
                    if (x == "" || x == " ")
                    {
                        b.innerHTML = "用户名不能为空";
                        return false;
                    }
                    else if(txt1!=txt2)
                    { 
                        b.innerHTML = "两次密码输入不一样!请重新输入";
                        return false;
                    }
                    else
                    {
                         b.innerHTML = "恭喜你注册成功!";
                        return true;
                    }
                }
                </script>
                 <script type="text/javascript">
                    function yanzheng()
                    {
                        var userName = document.getElementById("UserId").value;                   
                        ajax("/Home/Check?UserId=" + userName, function (resText)
                        {
                            if (resText == "OK")
                            {
                                document.getElementById("me").innerHTML = "有效用户ID";
                            }
                            else if (resText == "error")
                            {
                                document.getElementById("me").innerHTML = "无效用户ID";
                            }
                        });
                     }
            </script>

         

Model层方法代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication5注册验证.Models
{
    public class ZhuceBF
    {
        private mydboDataContext _Context = new mydboDataContext();
        public List<zhuce> Select()
        {
            return _Context.zhuce.ToList();
        }
        public zhuce Selectbyid(string id)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
        }



        public bool Selectid(string id)
        {
            var query = _Context.zhuce.Where(p => p.ID == id);
            if (query.Count() > 0)
            {
                return true;
                
            }
            return false;
        }


        public void Insert(string id,string username,string userpwd)
        {
            if (username == "" || username == " ")
            {
            }
            else
            {
                zhuce data = new zhuce();
                data.ID = id;
                data.Username = username;
                data.Userpwd = userpwd;
                _Context.zhuce.InsertOnSubmit(data);
                _Context.SubmitChanges();
            }
        }

        public void Update(string id, string username, string userpwd)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                zhuce data = query.First();
                data.ID = id;
                data.Username = username;
                data.Userpwd = userpwd;
            }
            _Context.SubmitChanges();
        }

        public void Delete(string id)
        {
            var query = _Context.zhuce.Where(p=>p.ID==id);
            if (query.Count()>0)
            {
                zhuce data = query.First();
                _Context.zhuce.DeleteOnSubmit(data);
                _Context.SubmitChanges();
            }
        }
    }
}

 

posted @ 2015-07-15 18:33  Yusarin  阅读(266)  评论(0编辑  收藏  举报