Javascript表单验证返回

Javascript form validation return

本文关键字:返回 验证 表单 Javascript      更新时间:2023-09-26

我对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,那么其他两个验证函数check姓氏()和checkMobile()将运行还是程序将在第一个验证函数处停止?

我的问题的原因是,在所有验证函数返回后,我想添加另一个函数来将所有内容保存到文件中。但是,只有在所有表单都经过验证后才应该这样做。任何帮助都是非常感激的,谢谢提前。

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

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);
    }
}

一个简单的解决方案(不忽略其他检查函数)应该是:

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;
}

程序将在第一个返回false的方法处停止,因为它们都包含在彼此中。因此,如果checkname()返回false,则validation()将返回false,其他函数也将返回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;
}

如果checkName(submit_name)返回false则

if(checkName(submit_name))将变成if(false),因此条件将为假,因此if-condition代码将不执行。并将继续执行。

这将适用于所有if条件。