Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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表单验证返回_Javascript_Html_Function_Validation - Fatal编程技术网

Javascript表单验证返回

Javascript表单验证返回,javascript,html,function,validation,Javascript,Html,Function,Validation,我对javascript有点陌生,我开始了解这种编码机制是如何工作的。我创建了一个带有多个字段的简单html表单。我使用javascript从字段中获取数据,并通过一些验证函数对其进行验证。以下代码是我当前使用的javascript: function Validation() { submit_name = document.getElementById('txt_Name').value; submit_surname = document.getElementByI

我对javascript有点陌生,我开始了解这种编码机制是如何工作的。我创建了一个带有多个字段的简单html表单。我使用javascript从字段中获取数据,并通过一些验证函数对其进行验证。以下代码是我当前使用的javascript:

function Validation()  
{  
    submit_name = document.getElementById('txt_Name').value;
    submit_surname = document.getElementById('txt_Surname').value;
    submit_mobilenumber = document.getElementById('MobileNumber').value;


    if(checkName(submit_name))
        {
    if(checkSurname(submit_surname))
            {
    if(checkMobile(submit_mobilenumber))
                {
                } 
            }
        }
    return false;
}
我的问题是:在这段代码中,主函数(Validation())将逐个遍历所有单个函数

例如,如果checkName()函数返回false,那么其他两个验证函数checkNames()和checkMobile()是否会运行,或者程序是否会在第一个验证函数处停止


我的问题的原因是,在所有验证函数都通过返回后,我想添加另一个函数来将所有内容保存到文件中。但是,只有在所有表单都经过验证后,才能执行此操作。如有任何帮助,我们将不胜感激

程序将在返回false的第一个方法处停止,因为它们都包含在彼此中。因此,如果
checkname()
返回false,
validation()
将返回false,其他函数也是如此

// if false, validate() returns false. if true, checksurname() called
if(checkName(submit_name))
{
    // if false, validate() returns false. if true, checkMobile() called
    if(checkSurname(submit_surname))
    {
        // if false, validate() returns false
        if(checkMobile(submit_mobilenumber))
        {
        }
    }
}
return false;
尽管如dreamweiver所说,个体验证会更好:

if(checkname()&&checkmobile()&&checksurname())
{
    return true;
}
else
{
    return false;
}
验证的简单解决方案(不忽略其他检查功能)应该是:

function Validation()  
{  
    var booleanValidation = false;
    var check_submit_name = checkName(document.getElementById('txt_Name').value);
    var check_submit_surname = checkSurname(document.getElementById('txt_Surname').value);
    var check_submit_mobilenumber = checkMobile(document.getElementById('MobileNumber').value);

    if (check_submit_name === false || check_submit_surname === false || check_submit_mobilenumber === false) {
        booleanValidaation = false;
    } else if (check_submit_name === true && check_submit_surname === true && check_submit_mobilenumber === true) {
        booleanValidaation = true;
    }

    return booleanValidation;
}
如果
checkName(submit\u name)
将返回false,则

if(checkName(submit_name))
将变为
if(false)
so条件将为false,因此if条件内的代码将不会执行。执行将持续不断


这将适用于所有if条件

我宁愿使用一个验证器,它返回每个包含错误的字段,这样您就可以向用户显示哪些字段填写错误

function Validation() {
    var submit_name = document.getElementById('txt_Name').value,
        submit_surname = document.getElementById('txt_Surname').value,
        submit_mobilenumber = document.getElementById('MobileNumber').value,
        errors = [];

    if(!checkName(submit_name)) {
        errors.push({
            id: 'txt_Name',
            message: 'Name is required'
        });
    }

    if(!checkSurname(submit_surname)) {
        errors.push({
            id: 'txt_Surname',
            message: 'Surname is required'
        });
    }

    if(!checkMobile(submit_mobilenumber)) {
        errors.push({
            id: 'MobileNumber',
            message: 'Mobile number is required'
        });
    }

    return errors;
}

var errors = Validation();

if(errors.length === 0) {
    // No errors, do your stuff
} else {
    // Loop errors and do something
    for(var i = 0; i < errors.length; i++) {
        // can mark the element with document.getElementById(errors[i].id)
        alert(errors[i].message);
    }
}
函数验证(){
var submit_name=document.getElementById('txt_name')。值,
submit_姓氏=document.getElementById('txt_姓氏')。值,
submit_mobilenumber=document.getElementById('mobilenumber')。值,
误差=[];
如果(!检查名称(提交名称)){
错误。推({
id:'txt_Name',
消息:“名称是必需的”
});
}
如果(!检查姓氏(提交姓氏)){
错误。推({
id:'txt_姓氏',
消息:“姓氏是必需的”
});
}
如果(!检查手机(提交手机号码)){
错误。推({
id:'MobileNumber',
消息:“需要手机号码”
});
}
返回错误;
}
var errors=Validation();
如果(errors.length==0){
//没有错误,做你的事
}否则{
//循环错误并做一些事情
对于(变量i=0;i
如果checkName()失败,那么它本身就不会进入If块,我希望我回答了你的问题。我建议你对字段进行单独验证,而不是嵌套条件。最明显的是,如果第一个函数返回false,你的验证函数将返回false,如果station不执行,则在其内部编写代码。这是一种糟糕的逻辑。单独验证字段时,可以一次提示用户所有验证错误,而不是提示用户更正姓名等。最后,重要的是用户的体验,所以要明智地一直这样做。这会改进我的回答,为每个元素添加一些错误消息。使用我的,你可以显示“请填写所有输入”之类的内容。这两种解决方案都不错,只取决于你想要的具体程度。不错的一个伴侣+1是的,这取决于开发者的需求。我个人更喜欢在验证表单时使用类似的东西。