Validation jQuery验证程序将字段标记为有效

Validation jQuery验证程序将字段标记为有效,validation,jquery,jquery-plugins,Validation,Jquery,Jquery Plugins,是否可以使用jqueryvalidator将字段标记为有效字段,或者以另一种方式来远程其关联的错误映射。我使用两种自定义ajax验证方法,这意味着我的验证方法不能返回true或false。因此,我试图用错误{field:false}(并标记validator.invalid.field=false)隐藏错误,但我猜仍然保留了一些错误映射,这会阻止表单在onsubmit:true时提交。如果我设置onsubmit:true,即使其中一个ajax验证字段显示错误,表单也会提交 $(document)

是否可以使用jqueryvalidator将字段标记为有效字段,或者以另一种方式来远程其关联的错误映射。我使用两种自定义ajax验证方法,这意味着我的验证方法不能返回true或false。因此,我试图用错误{field:false}(并标记validator.invalid.field=false)隐藏错误,但我猜仍然保留了一些错误映射,这会阻止表单在onsubmit:true时提交。如果我设置onsubmit:true,即使其中一个ajax验证字段显示错误,表单也会提交

$(document).ready(function () {

    var validator = $(".formframe").validate({
        groups: { phone: "phone_1 phone_2 phone_3" },
        rules: {
            FirstName: { required: true },
            LastName: { required: true },
            Email: { required: true, email_custom: true },
            phone_1: { required: true }, // these two fields get a phone validation rule
            phone_2: { required: true }, // added upon validation triggered by phone_3 
            phone_3: { required: true, phone: true },
            Address1: { required: true },
            City: { required: true },
            PostalZipCode: { required: true, postalcode: true },
            CustField1: { required: true, range: [1950,2012] }
        },
        messages: {
            FirstName: "S'il vous plaît entrer votre prénom",
            LastName: "S'il vous plaît entrer votre nom de famille",
            Email: "S'il vous plaît entrer une adresse email valide",
            phone_1: "S'il vous plaît entrer votre numéro de téléphone",
            phone_2: "S'il vous plaît entrer votre numéro de téléphone",
            phone_3: {
                required: "S'il vous plaît entrer votre numéro de téléphone",
                phone: "Numéro de téléphone doit être réel"
            },
            Address1: "S'il vous plaît, entrer votre adresse",
            City: "S'il vous plaît, entrer votre ville",
            PostalZipCode: {
                required: "S'il vous plaît entrer votre code postal",
                postalcode: "S'il vous plaît entrer votre code postal"
            },
            CustField1: {
                required: "S'il vous plaît entrer votre dernière année d'études",
                range: "Entrer une année de l'obtention du diplôme réel"
            }
        },
        onfocusout: function(element) { $(element).valid(); },
        errorPlacement: function(error, element) {
            if (element.attr("name") == "phone_1" || element.attr("name") == "phone_2" || element.attr("name") == "phone_3") {
                error.insertAfter("#phone_3");
            } else {
                error.insertAfter(element);
            }
        },
        onkeyup: false,
        onsubmit: true
    }); console.log(validator);

    // custom email validation method
    $.validator.addMethod("email_custom", function(value, element) {
        var verify = $.tdverify({
            // Use TowerData's domain authentication for best security 
            // or set your license key here
            'license' : 'xxx', 

            // This is the data to validate
            // The values here are the IDs of the input fields.
            // See demo.js on how to use jQuery or DOM to specify fields.
            'email' : $('#Email'),
        });

        // These are the API settings. 
        verify.set({
            'settings' : {
                'valid_email' : 'mailbox',  // Enable email validation of mailbox.
                                            // Use value of 'syntaxdomain' for syntax and 
                                            // domain validation only.
                'timeout'    : 5            // Set timeout to 5 seconds
            }
        });

        // because this function uses a callback, we can't return true to the validation
        // method, instead we set validator.invalid.[field name] = false so the form submits
        verify.process({
            'onSuccess' : function(data, textStatus, xhr) {
                if (typeof data.email == "object" && data.email.ok == false) {
                    //validator.showErrors({"Email": data.email.status_desc});
                    validator.defaultShowErrors();
                } else {
                    validator.showErrors({"Email": false});
                    delete validator.invalid["Email"];
                    console.log(validator);
                } 
            },
            'onError' : function() {
                validator.showErrors({"Email": "Email validation timeout"});
            }
        });
    });

    // custom phone validation method
    $.validator.addMethod("phone", function(value, element) {

        // concatenate phone number parts into a single hidden field
        $("#phone").val($("#phone_1").val() + $("#phone_2").val() + $("#phone_3").val());
        // initially only phone_3 has validation enabled, this allows the phone number to be
        // typedfrom start to finish, adding the phone class to phone_1 and phone_2
        // will cause them to be validated if they are changed
        $("#phone_1,#phone_2").addClass("phone");

        var verify = $.tdverify({
            // Use TowerData's domain authentication for best security 
            // or set your license key here
            'license' : 'xxx', 

            // This is the data to validate
            // The values here are the IDs of the input fields.
            // See demo.js on how to use jQuery or DOM to specify fields.
            'phone' : $("#phone")
        });

        // These are the API settings. 
        verify.set({
            'settings' : {
                'valid_phone' : true,       // Enable phone validation
                'timeout'    : 5            // Set timeout to 5 seconds
            }
        });

        verify.process({
            'onSuccess' : function(data, textStatus, xhr) {
                if (typeof data.phone != "undefined" && data.phone.ok == false) {
                    //validator.showErrors({"phone": data.phone.status_desc});
                    validator.defaultShowErrors();
                } else {
                    validator.showErrors({"phone_3": false});
                    delete validator.errorMap["phone_1"];
                    delete validator.errorMap["phone_2"];
                    delete validator.errorMap["phone_3"];
                    delete validator.invalid["phone_1"];
                    delete validator.invalid["phone_2"];
                    delete validator.invalid["phone_3"];
                    console.log(validator);
                }               
            },
            'onError' : function() {
                validator.showErrors({"phone_3": "Phone validation timeout"});
            }
        })
    });

    $.validator.addMethod("postalcode", function(postalcode, element) {
        if(postalcode.length == 6 && !parseInt(postalcode)){
            // no space in postal code
            var s = postalcode.substring(0,3) + ' ' + postalcode.substring(3);
            element.value = s;
        }
        return this.optional(element) || postalcode.match(/(^[ABCEGHJKLMNPRSTVXYabceghjklmnpstvxy]{1}\d{1}[A-Za-z]{1} ?\d{1}[A-Za-z]{1}\d{1})$/);
    });

    // phone number auto-tabbing
    $('#phone_1').autotab({ target: 'phone_2', format: 'numeric' });
    $('#phone_2').autotab({ target: 'phone_3', format: 'numeric', previous: 'phone_1' });
    $('#phone_3').autotab({ previous: 'phone_2', format: 'numeric' });
    $("#PostalZipCode").autotab_filter('alphanumeric');

    // allows only numeric input:
    // this keydown binding won't allow letters at all, the above autotab
    // numeric format simply removes anything typed that isn't within 0-9
    $("#phone_1,#phone_2,#phone_3,#CustField1").keydown(function(event) {
        // Allow: backspace, delete, tab, escape, and enter
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
            // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) || 
            // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
                // let it happen, don't do anything
                return;
        } else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                event.preventDefault(); 
            }
        }
    });

    // automatically capitalize letters in postal code  
    $("#PostalZipCode").keyup(function() {
        $(this).val(($(this).val()).toUpperCase());
    });
});
$(文档).ready(函数(){
var验证程序=$(“.formframe”).validate({
分组:{phone:{phone_1 phone_2 phone_3},
规则:{
名字:{required:true},
LastName:{必需:true},
电子邮件:{required:true,Email_custom:true},
phone_1:{required:true},//这两个字段获取电话验证规则
phone_2:{required:true},//在phone_3触发验证时添加
phone_3:{必需:true,phone:true},
地址1:{必需:true},
城市:{必需:true},
PostalZipCode:{required:true,postalcode:true},
CustField1:{必需:true,范围:[19502012]}
},
信息:{
名字:“你的名字是什么?”,
姓氏:“你的名字叫什么?”,
电子邮件:“您的电子邮件地址有效”,
电话1:“你的电话号码是多少?”,
电话2:“你的电话号码是多少”,
电话(3){
要求:“您的电话号码是多少?”,
电话:“电话号码”
},
地址1:“你的计划,地址”,
城市:“你的军队,你的城市”,
邮政编码:{
所需信息:“您的邮政编码”,
邮政编码:“邮政编码”
},
卡斯特菲尔德1:{
要求:“你的计划是什么?”,
射程:“你可以从你的眼睛里看到我”
}
},
onfocusout:函数(元素){$(元素).valid();},
errorPlacement:函数(错误,元素){
if(element.attr(“name”)=“phone_1”| | element.attr(“name”)=“phone_2”| | element.attr(“name”)=“phone_3”){
错误。插入后面的(“#phone_3”);
}否则{
错误。插入符(元素);
}
},
onkeyup:false,
问题:是的
});console.log(验证器);
//自定义电子邮件验证方法
$.validator.addMethod(“电子邮件\自定义”,函数(值,元素){
var verify=$.tdverify({
//使用TowerData的域身份验证实现最佳安全性
//或者在此处设置您的许可证密钥
‘许可证’:‘xxx’,
//这是要验证的数据
//这里的值是输入字段的ID。
//有关如何使用jQuery或DOM指定字段的信息,请参见demo.js。
‘email’:$(‘email’),
});
//这些是API设置。
verify.set({
“设置”:{
'valid_email':'mailbox',//启用邮箱的电子邮件验证。
//将“syntaxdomain”的值用于语法和
//仅限域验证。
“超时”:5//将超时设置为5秒
}
});
//由于此函数使用回调,因此无法将true返回到验证
//方法,而是设置validator.invalid.[field name]=false,因此表单将提交
验证过程({
“onSuccess”:函数(数据、文本状态、xhr){
if(type of data.email==“object”&&data.email.ok==false){
//validator.showErrors({“Email”:data.Email.status_desc});
validator.defaultRors();
}否则{
validator.rors({“Email”:false});
删除验证程序。无效[“电子邮件”];
console.log(验证器);
} 
},
'onError':函数(){
validator.rors({“Email”:“Email validation timeout”});
}
});
});
//自定义电话验证方法
$.validator.addMethod(“电话”),函数(值,元素){
//将电话号码部分连接到单个隐藏字段中
$(“#phone”).val($(“#phone_1”).val()+$(“#phone_2”).val()+$(“#phone_3”).val();
//最初,只有phone_3已启用验证,这允许输入电话号码
//键入从开始到结束,将phone类添加到phone_1和phone_2
//如果更改,将导致对其进行验证
$(“#phone_1,#phone_2”).addClass(“phone”);
var verify=$.tdverify({
//使用TowerData的域身份验证实现最佳安全性
//或者在此处设置您的许可证密钥
‘许可证’:‘xxx’,
//这是要验证的数据
//这里的值是输入字段的ID。
//有关如何使用jQuery或DOM指定字段的信息,请参见demo.js。
电话:$(“电话”)
});
//这些是API设置。
verify.set({
“设置”:{
'valid_phone':true,//启用电话验证
“超时”:5//将超时设置为5秒
}
});
验证过程({
“onSuccess”:函数(数据、文本状态
submitHandler: function(form) {
    var emptyFields = false;
    $(":input").each(function() {
        if ($(this).val() === "") emptyFields = true;
    });
    if ($.isEmptyObject(validator.invalid) && !emptyFields) { // all is valid, submit
        form.submit();
    } else {
        alert("Please correct some errors");
    }
}