当jQuery验证的远程故障开放模式 [英] Open modal when jQuery validation remote fails

查看:109
本文介绍了当jQuery验证的远程故障开放模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery验证插件,和它的伟大工程。

我希望能够同时显示一条消息,并触发模式(警报()中的例子),每当远程AJAX失败。我不知道如何做到这两点。现在,它会触发警报()的预期,也追加了错误消息请解决这个问题领域,这应该是我自己的自定义错误消息。

下面是我有:

  $(#住址形式)。验证({

        errorElement:跨越,

        规则:{

            navn:{
                要求:真实,
                MINLENGTH:5,
                最大长度:25
            },
            TLF:{
                要求:真实,
                数字:真正的,
                MINLENGTH:8,
                远程: {
                    网址:/ AJAX / check_tlf
                    类型:后
                }
            }
        },

        消息:{

            navn:字段名称是必需的,
            TLF:{
                要求:现场TLF是必需的!,
                遥控器:函数(){//我想藏汉添加消息,而不仅仅是警报

                    警报(失败 -  TLF已经采取了!);
                }
            }

        },
        submitHandler:功能(形式){
            doSomethingGreatOnSuccess();
        },

        errorPlacement:功能(错误,元素){
            error.appendTo(element.parent());
        }

    });
 

解决方案

下面是我如何得到它的工作:

  jQuery.validator.addMethod(利用,功能​​(价值要素){
    VAR isSuccess = TRUE;

    $阿贾克斯({
        网址:/无济于事,
        键入:POST,
        数据:{TLF:值},
        异步:假的,
        成功:函数(MSG){
            isSuccess =味精===真?真假;
            如果(!isSuccess){
                $('#myModal)显示()。
            }
        }
    });
    //返回isSuccess;
});
 

和的规则:

  TLF:{
                要求:真实,
                数字:真正的,
                MINLENGTH:8,
                果:真
            }
 

和消息:

  TLF:{
                要求:必须输入的电话号码,
                果:电话号码已被使用!
            },
 

对于任何类似的问题,我发现很多颇有帮助,在这里: http://stackoverflow.com/a/2628442/839716

I'm using the jQuery validation plugin, and it works great.

I want to be able to both display a message and trigger a modal (alert() in the example) whenever the remote ajax fails. I can't figure out how to do both. Now, it triggers the alert() as expected, but also appends the error message "Please fix this field", which should be my own custom error message.

Here's what I've got:

$("#adresse-form").validate({

        errorElement: "span",

        rules: {

            navn: {
                required: true,
                minlength: 5,
                maxlength: 25
            },
            tlf: {
                required: true,
                digits: true,
                minlength: 8,
                remote: {
                    url: "/ajax/check_tlf",
                    type: "post"
                }
            }
        },

        messages: {

            navn: "Field Name is required",
            tlf: {
                required: "Field tlf is required!",
                remote: function () { // i want to add the message aswell, not just the alert

                    alert("failed - tlf is already taken!");
                }
            }

        },
        submitHandler: function(form) {
            doSomethingGreatOnSuccess();
        },

        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        }

    });

解决方案

Here is how I got it working:

jQuery.validator.addMethod("avail",function(value,element){
    var isSuccess = true;

    $.ajax({
        url: "/avail",
        type: 'POST',
        data: { tlf: value },
        async: false,
        success: function (msg) {
            isSuccess = msg === "true" ? true : false;
            if (!isSuccess) {
                $('#myModal').reveal();
            }
        }
    });
    // return isSuccess;
});

And in the rules:

tlf: {
                required: true,
                digits: true,
                minlength: 8,
                avail: true
            }

And the messages:

tlf: {
                required: 'Must enter phone number',
                avail: 'Phone number is already taken!'
            },

To anyone with similar problems, I found quite alot of help here: http://stackoverflow.com/a/2628442/839716

这篇关于当jQuery验证的远程故障开放模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆