javascript、ajax验证

 <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();

            }

                  注意返回值的接收

posted @ 2015-06-02 11:07  Yida_婷婷  阅读(272)  评论(0编辑  收藏  举报