博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript、ajax验证
阅读量:5360 次
发布时间:2019-06-15

本文共 3652 字,大约阅读时间需要 12 分钟。

 <script type="text/javascript">

        function username() {

            $('#msg1').remove();

            var username = $("#Username").val();

            var reg = new RegExp("^([A-Z a-z u4E00-u9FA5]{4,16})$");

            if (username == null || username == "") {

                  $('#Username').after("<b id='msg1' >" + "用户名不能为空!" + "</b>");

                return false;            

    }

            else if (!reg.exec(username)) {

                $('#Username').after("<b id='msg1'>" + "用户名只能用中文、英文、数字、下划线、4-16个字符!" + "</b>");

                 return false;

            }

             else {

                return true;

            }

        }

        function password() {

            $('#msg1').remove();

            var password = $("#Password").val();

            if (password == null || password == "") {

                 $('#Password').after("<b id='msg1'>" + "密码不能为空!" + "</b>");

                return false;

            }

            else {

                return true;

            }

        }

        function confirmPassword() {

            $('#msg1').remove();

            var password = $("#Password").val();

            var comfirmpassword = $("#ConfrimPassword").val();

            if (comfirmpassword == null || comfirmpassword == "") {

                  $('#ConfrimPassword').after("<b id='msg1'>" + "确认密码不能为空!" + "</b>");

                return false;

            }

            else if (comfirmpassword != password) {

                $('#ConfrimPassword').after("<b id='msg1'>" + "两次密码输入不一致!" + "</b>");

                return false;

            }

            else {

                return true;

            }

        }

        function telephone() {

            $('#msg1').remove();

            var telephone = $("#Telephone").val();

            var reg = new RegExp("^(13[0-9]{9})|(14[0-9])|(18[0-9])|(15[0-9][0-9]{8})$");

            if (telephone == null || telephone == "") {

                $('#Telephone').after("<b id='msg1'>" + "电话号码不能为空!" + "</b>");

                return false;

            }

            else if (!reg.exec(telephone)) {

                $('#Telephone').after("<b id='msg1'>" + "电话号码格式不正确!" + "</b>");

                 return false;

            }

            else {

                return true;

            }

        }

        function email() {

            $('#msg1').remove();

            var email = $("#Email").val();

            var reg = new RegExp("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$");

            if (email == null || email == "") {

                $('#Email').after("<b id='msg1'>" + "邮箱不能为空!" + "</b>");

                 return false;

            }

            else if (!reg.exec(email)) {

                $('#Email').after("<b id='msg1'>" + "邮箱格式不正确!" + "</b>");

                 return false;

            }

            else {

                return true;

            }

        }

        var returnvalue = false;

        function registeryanzheng() {

            var username = $("#Username").val();

            var quanxian = $("#DropDownListPermission").val();

            var a = "用户名或权限存在";  

           $.ajax({

                type: "POST",

                url: "register.aspx?action=ajax",

                data: "z_username=" + username + "&&z_permission=" + quanxian,

                success: function (msg) {

                    $('#msg1').remove();

                    $("#Submit").attr("disabled", false);

                    if (msg.indexOf(a) > -1) {

                        $('#DropDownListPermission').after("<b id='msg1'>" + msg + "</b>");

                        //判断按钮是否可用

                        $("#Submit").attr("disabled", true);

                        returnvalue = false;

                    }

                    else {  

                       $("#Submit").attr("disabled", false);

                        returnvalue = true;

                    }

                }

            });

 }

        function yanzheng() {

            if (!username()) {

                return false;

            }

            if (!password()) {

                return false;

            }

            if (!confirmPassword()) {

                return false;

            }

            if (!telephone()) {

                return false;

            }

            if (!email()) {

                return false;

            }

            if (!registeryanzheng()) {

                registeryanzheng();

                return returnvalue;

            }

            else {

                return true;

            }

        }

    </script>

 

后台:

if (!String.IsNullOrEmpty(Request["action"]) && Request["action"] == "ajax")

            {

                string username = Request["z_username"];

                string permission = Request["z_permission"];

                SqlConnection conn = new SqlConnection(url);

                conn.Open();

                try  

               {                    

        SqlCommand cmd = new SqlCommand();

                    DataTable dt = new DataTable();

                    cmd.CommandText = "select * from zmt_user where z_username = '" + username + "'"

                        + " and z_permission = '" + permission + "'";

                    cmd.Connection = conn;

                    SqlDataAdapter sda = new SqlDataAdapter(cmd);

                    sda.Fill(dt);

                    if (dt.Rows.Count > 0)

                    {

                        Response.Write("用户名或权限存在");

                        //System.Threading.Thread.Sleep(2000);

                    }

                    else {

                        Response.Write("不存在");

                    }

                }  

               catch (Exception ex)

                {

                    Console.WriteLine("error");

                }

                finally

                {

                    Response.End();

                    conn.Close();

                }

                Response.End();

            }

                  注意返回值的接收

转载于:https://www.cnblogs.com/Yida-Tingting/p/4545887.html

你可能感兴趣的文章
MySQLdb & pymsql
查看>>
zju 2744 回文字符 hdu 1544
查看>>
【luogu P2298 Mzc和男家丁的游戏】 题解
查看>>
前端笔记-bom
查看>>
上海淮海中路上苹果旗舰店门口欲砸一台IMAC电脑维权
查看>>
Google透露Android Market恶意程序扫描服务
查看>>
给mysql数据库字段值拼接前缀或后缀。 concat()函数
查看>>
迷宫问题
查看>>
【FZSZ2017暑假提高组Day9】猜数游戏(number)
查看>>
泛型子类_属性类型_重写方法类型
查看>>
练习10-1 使用递归函数计算1到n之和(10 分
查看>>
Oracle MySQL yaSSL 不明细节缓冲区溢出漏洞2
查看>>
Code Snippet
查看>>
zoj 1232 Adventure of Super Mario
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
Redis常用命令
查看>>
[转载]电脑小绝技
查看>>
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>