Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Javascript jQuery-仅当所有表单字段都有效时才运行加载图形函数_Javascript_Jquery_Validation - Fatal编程技术网

Javascript jQuery-仅当所有表单字段都有效时才运行加载图形函数

Javascript jQuery-仅当所有表单字段都有效时才运行加载图形函数,javascript,jquery,validation,Javascript,Jquery,Validation,我有下面的javascript代码来验证一个带有用户名和密码的简单登录表单——如果这起作用的话,就是asp.net自定义验证器的客户端验证 function userValidation(source, arguments) { if (arguments.Value != "" && arguments.Value != "User name") { if($(source).parents(".loginPanel").find

我有下面的javascript代码来验证一个带有用户名和密码的简单登录表单——如果这起作用的话,就是asp.net自定义验证器的客户端验证

    function userValidation(source, arguments) {
        if (arguments.Value != "" && arguments.Value != "User name") {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }


    function passwordValidation(source, arguments) {
        var passVal = /^((?![<>]).){1,64}$/;
        if (arguments.Value != "" && arguments.Value != "Password" && passVal.test(arguments.Value)) {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }
问题是,即使页面无效,加载函数也会运行

有什么想法我可以把它包括进去,这样它只有在一切都有效的情况下才能运行吗


谢谢

无需编写更多代码,您可以执行以下操作:

var x = [];
function userValidation(source, arguments) {
        if (arguments.Value != "" && arguments.Value != "User name") {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
            x.push(true);
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }


    function passwordValidation(source, arguments) {
        var passVal = /^((?![<>]).){1,64}$/;
        if (arguments.Value != "" && arguments.Value != "Password" && passVal.test(arguments.Value)) {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
            x.push(true);
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }

function loading(){
    var confirmx = true;
    for (var i = 0; i < x.length; i++) {
        if (x[i] != true) {
            confirmx = false;
        }
    }
    if (confirmx){
        $(".loginPanel").on("click", "input[id*='Login']", function() {
            loadingPage2("", "Logging in...");
        });
    }
}
loading();  // run it!
var x=[];
函数userValidation(源、参数){
if(arguments.Value!=“&&arguments.Value!=“用户名”){
if($(source).parents(“.loginPanel”).find(“.errorAsterisk[style*='visible']”)。length==0){
$(source).parents(“.loginPanel”).css({“背景色”:“});
}
arguments.IsValid=true;
x、 推送(真);
}否则{
$(来源).parents(“.loginPanel”).css({“背景色”:“#ECC8C8”});
arguments.IsValid=false;
}
}
函数密码验证(源、参数){
var passVal=/^((?![]){1,64}$/;
if(arguments.Value!=“&&arguments.Value!=“Password”&&passVal.test(arguments.Value)){
if($(source).parents(“.loginPanel”).find(“.errorAsterisk[style*='visible']”)。length==0){
$(source).parents(“.loginPanel”).css({“背景色”:“});
}
arguments.IsValid=true;
x、 推送(真);
}否则{
$(来源).parents(“.loginPanel”).css({“背景色”:“#ECC8C8”});
arguments.IsValid=false;
}
}
函数加载(){
var confirmx=真;
对于(变量i=0;i

您唯一需要检查的是,此代码在所有验证函数之后运行。因此,请将这段代码放在脚本的末尾。

有很多方法可以实现这一点。最简单的方法是设置一个全局变量e.q.
success
。在事件处理程序中,只需检查此变量的值。

Hmmmm,我担心您会这样说,因为我还有很多地方需要添加加载图形。是否可以将变量设置为数组并检查数组中的所有条目是否为真?如果不需要知道哪个值为假,请使用以下命令:
var x=[]
和每个函数的外接程序
x.push(真)。您需要知道哪个值失败了吗?(如果我知道,我可以更新答案)当然。。。我已经添加到数组中,如果它为false,然后在加载函数中检查x.length,然后继续。。。谢谢
var x = [];
function userValidation(source, arguments) {
        if (arguments.Value != "" && arguments.Value != "User name") {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
            x.push(true);
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }


    function passwordValidation(source, arguments) {
        var passVal = /^((?![<>]).){1,64}$/;
        if (arguments.Value != "" && arguments.Value != "Password" && passVal.test(arguments.Value)) {
            if($(source).parents(".loginPanel").find(".errorAsterisk[style*='visible']").length==0) {
                $(source).parents(".loginPanel").css({ "background-color": "" });
            }
            arguments.IsValid = true;
            x.push(true);
        } else {
            $(source).parents(".loginPanel").css({ "background-color": "#ECC8C8" });
            arguments.IsValid = false;
        }
    }

function loading(){
    var confirmx = true;
    for (var i = 0; i < x.length; i++) {
        if (x[i] != true) {
            confirmx = false;
        }
    }
    if (confirmx){
        $(".loginPanel").on("click", "input[id*='Login']", function() {
            loadingPage2("", "Logging in...");
        });
    }
}
loading();  // run it!