必须至少完成两个字段中的一个验证

时间:2014-08-15 02:01:43

标签: javascript validation

首先,我使用jquery验证插件

我在JavaScript中进行了以下验证:

var m = mob.substr(0,2);
var l = land.substr(0,2);

if (mob == '' && land == '') {
    alert("You must enter at least one phone number");
    return false;
}
else if (mob.length != 10 || m != "04") {
    alert("Mobile number is invalid. Must be a valid australian mobile number.");
    return false;
}
else if (land.length != 10 || l == "04" || l == "01" || l == "05" || l == "06" || l == "09" || l == "00") {
    alert("Landline is invalid. Must be a valid australian landline number.");
    return false;
}

正如标题所示,我希望用户只能输入一个或两个字段,但不要将两个字段都留空。

如何将其纳入验证?

4 个答案:

答案 0 :(得分:1)

var m = mob.substr(0,2);
var l = land.substr(0,2);

if (mob === '' && land === '') {
    alert("You must enter at least one phone number");
    return false;
}
if (mob !== '' && (mob.length !== 10 || m !== "04")) {
    alert("Mobile number is invalid. Must be a valid australian mobile number.");
    return false;
}
if (land !== '' && (land.length !== 10 || l === "04" || l === "01" || l === "05" || l === "06" || l === "09" || l === "00")) {
    alert("Landline is invalid. Must be a valid australian landline number.");
    return false;
}
return true;

这里只添加一个条件到最后两个条件。并使它们作为独立的if语句。因此,所有这些都将被评估。

答案 1 :(得分:0)

只是一个想法......

function validate () {

    var sMobile = ...;
    var sLand   = ...;

    var lines = parsePhoneLines(sMobile, sLand);

    if (lines.errors) {
        for (var i = 0, length = lines.errors.length; i < length; i++) {
            alert (lines.errors[i]);
        }
        return false;
    } else {
        return true;
    }

}

function parsePhoneLines (sMobile, sLand) {
    var mobile = parseMobile(sMobile),
        land   = parseLand(sLand);

    if (mobile.errors && land.errors) {
        return {
            errors: mobile.errors.concat(land.errors)
        };
    } else {
        return {
            land:   land.value,
            mobile: mobile.value
        };
    }
}

function parseMobile (s) {
    var errors = getErrorsForMobile(s);

    if (errors.length === 0) {
        return { value: s };
    } else {
        return { errors: errors };
    }
}

function parseLand (s) {
    var errors = getErrorsForLand(s);

    if (errors.length === 0) {
        return { value: s };
    } else {
        return { errors: errors };
    }
}

答案 2 :(得分:0)

试试这个:

var m = mob.substr(0,2);
var l = land.substr(0,2);

if (mob == '' && land == '') {
    alert("You must enter at least one phone number");
    return false;
} else {
    if (mob != '') {
    if (mob.length != 10 || m != "04") {
        alert("Mobile number is invalid. Must be a valid australian mobile number.");
        return false;
    }
    } else {
    if (land.length != 10 || l == "04" || l == "01" || l == "05" || l == "06" || l == "09" || l == "00") {
        alert("Landline is invalid. Must be a valid australian landline number.");
        return false;
    }
    }
}
return true;

只需要一个else子句到第一个if。

答案 3 :(得分:0)

var m = mob.substr(0,2);
var l = land.substr(0,2);
var flag=1;
if (mob == '' && land == '') {
    alert("You must enter at least one phone number");
    return false;
}
else if (mob.length != 10 || m != "04") {
    if(land.length>0)
     {
      //landline have value don not alert this
     }
     else
     {
        alert("Mobile number is invalid. Must be a valid australian mobile number.");
        flag=0;//means invalid
     }
}
else if (land.length != 10 || l == "04" || l == "01" || l == "05" || l == "06" || l == "09" || l == "00") {
    if(flag==1)
    {
        return true;// means mobile have value
    }
    alert("Landline is invalid. Must be a valid australian landline number.");
    return false;
}