加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax无刷新表单提交、验证码的使用

发布时间:2020-12-16 01:43:22 所属栏目:百科 来源:网络整理
导读:无刷新网页表单使用 1、给表单添加验证 //注册验证 $("#myform").valid([ { name: "reg_name",type: "text",isNull: "用户不能为空",onFocus: "请填写用户!" },{ name: "reg_email",type: "mail",isNull: "邮箱不能为空",onFocus: "邮箱格式(*@*.*)",error:

无刷新网页表单使用

1、给表单添加验证

    //注册验证
    $("#myform").valid([
        { name: "reg_name",type: "text",isNull: "用户不能为空",onFocus: "请填写用户!" },{ name: "reg_email",type: "mail",isNull: "邮箱不能为空",onFocus: "邮箱格式(*@*.*)",error: "邮箱格式不正确" },{ name: "reg_password",type: "password",isNull: "密码不能为空",onFocus: "请填写6-18位密码",error: "请填写6-18位密码" },{ name: "reg_confirm",type: "eq",error: "两次输入密码不一致",other: { to: "reg_password" } },{ name: "reg_code",type: "ajax",error: "验证码不正确",other: { url: "/Handlers/RegHandler.ashx" } }

    ],true);

2写入ajaxForm()


//提交注册信息
    function ajaxForm() {
        $.ajax({
            type: "POST",//设置请求发送的方式
            timeout: 30000,//设置服务器请求超时时间
            url: "/Handlers/UpHandler.ashx",//提交的地址
            data: $("#myform").serialize(),//序列化表单元素值
            beforeSend: function () {//表单提交前执行的函数
                alert("text");
                $("#errordiv").text("服务器超时,请稍后再试![关闭]");
                $("#errordiv").hide();
                $("#zzc").show();
                popupDiv("popdiv");
            },error: function () {//提交发生错误的时候执行的函数
                $("#infodiv").hide();
                $("#errordiv").show();
            },success: function (data) {//提交成功的时候执行的函数
                if (data == "success") {
                    $("#infodiv").hide();
                    $("#regsuccess").show();
                    setInterval(MyTimer,1000)
                } else {
                    $("#infodiv").hide();
                    $("#errordiv").text(data);
                    $("#errordiv").show();
                }
            }
        });
    }//)

3、后台接受采用一般处理程序
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //如果用户直接访问handler则拒绝
            if (context.Request.UrlReferrer == null)
            {
                context.Response.Write("请求错误!拒绝访问!");
                return;
            }
            //获取请求来路的完整的url
            string url = context.Request.UrlReferrer.AbsoluteUri;
            if (!url.Contains("/CompanyDishes/DishesBooks"))
            {
                context.Response.Write("请求错误!拒绝访问");
                return;
            }
            DishBook objBook = new Book()
            {
                CustomerName = context.Request.Params["CustomerName"],HotelName = context.Request.Params["HotelName"],ConsumeTime = Convert.ToDateTime(context.Request.Params["ConsumeTime"]),CustomerPhone = context.Request.Params["CustomerPhone"],CustomerEmail = context.Request.Params["CustomerEmail"],ConsumePersons = Convert.ToInt32(context.Request.Params["ConsumePersons"]),RoomType = context.Request.Params["selectRoomType"],Comments = context.Request.Params["Comments"] == "" ? "无" : context.Request.Params["Comments"],};
            try
            {
                //提交数据
                int count = new BookManager().AddBook(objBook);
                if (count > 0)
                    context.Response.Write("success");
                else
                {
                    context.Response.Write("error");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("提交失败!" + ex.Message);
                throw;
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读