javascript、ajax驗證

來源:互聯網
上載者:User

標籤:

 <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_-])[email protected]([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();

            }

                  注意傳回值的接收

javascript、ajax驗證

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.