Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor:jQueryValidation remote使用javascript函数_Javascript_Jquery Validate_Meteor - Fatal编程技术网

Meteor:jQueryValidation remote使用javascript函数

Meteor:jQueryValidation remote使用javascript函数,javascript,jquery-validate,meteor,Javascript,Jquery Validate,Meteor,我正在尝试使用jQueryValidation:remoteforMeteorJavaScript函数来验证注册表单上的电子邮件 Javascript验证: $("#register-form").validate({ rules: { email: { required: true, email: true, // remote: don't know what to do }

我正在尝试使用jQueryValidation:remoteforMeteorJavaScript函数来验证注册表单上的电子邮件

Javascript验证:

$("#register-form").validate({
    rules: {
        email: {
            required: true,
            email: true,
            // remote: don't know what to do
        }
    },
    messages: {
        email: {
            required: "Please enter your email",
            remote: "Please enter another one. This email is already exist"
        },
    }
    //...
});
流星功能:

Template.tmp_signup.events({
    'change #inpSignupEmail': function () {
        if (typeof console !== 'undefined') {
            var data = document.getElementById("inpSignupEmail").value;
            var findOut = UserCustomer.find({Email: data});
            if (findOut.fetch().length > 0) {
                console.log("this email is already exist. too bad");
            }
            else {
                console.log("this is a new email!");
            }
        }
    },  
}
到目前为止,我的“
change#inSignupEmail
”功能仍然有效,但当然,我得到的只是一个控制台日志:“这封电子邮件已经存在,太糟糕了”

是否可以将我的javascript函数用于jQueryValidation:remote


谢谢。

您不能在JavaScript中使用
remote
,因为
remote
用于远程检查服务器上的内容并读取响应

相反,您必须使用来编写自己的规则,并将其他JavaScript合并到一起

类似这样的,虽然我假设你的meteor功能已经正常工作了

jQuery.validator.addMethod("myMeteor", function(value, element) {
    Template.tmp_signup.events({
        'change #inpSignupEmail': function () {
            if (typeof console !== 'undefined') {
                var data = value;  // 'value' argument represents the value of your input
                var findOut = UserCustomer.find({Email: data});
                if (findOut.fetch().length > 0) {
                    return false;  // fails - display error
                }
                else {
                    return true;  // passes - no message
                }
            }
        }
    }  
}, "this email is already exist. too bad");
.validate()中声明

$("#register-form").validate({
    rules: {
        email: {
            required: true,
            email: true,
            myMeteor: true
        }
    }, ......

在对@Sparky关于使用
addMethod()
的答案进行讨论之后,我发现这个代码片段在我的例子中运行得非常好:

jQuery.validator.addMethod("myMeteor", function (value, element) {
    if (typeof console !== 'undefined') {
        var data = value;  // 'value' argument represents the value of your input
        var findOut = UserCustomer.find({Email: data});
        if (findOut.count() > 0) {
            return false;  // fails - display error    
        }
        else {
            return true;  // passes - no message
        }
    }
});

附言:感谢@Sparky的创意。

感谢您使用addMethod()方法的创意。显然,我必须修改我的流星功能一点,使其工作。我会马上发布我的工作解决方案。