验证不适用于所有字段

时间:2015-08-12 07:06:35

标签: javascript validation

我正在开发一个基本的Web应用程序,我想验证它的某些字段,并且有一些字段不是mandtory但是如果它在那里那么应该像url一样有效。

所以我的代码是

validate:function(attrs){

        var obTobeValidate={};
        var regexp = /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
        obTobeValidate.questionImageUrl=$("#addSolvedExampleContainer .solvedQuestionImage").val();
        obTobeValidate.solutionImageUrl=$("#addSolvedExampleContainer .solvedQuestionSolutionImage").val();
        obTobeValidate.videoUrl=$("#addSolvedExampleContainer .solvedQuestionVideoUrl").val();

        if(!attrs.title){
             alert("please mention title..");
             return false;
        }

        if(!attrs.question){
            alert("please enter the question");
            return false;
        }    


        if(!attrs.solution){
            alert("please enter the solution for the question..");
            return false;
        }

        if (obTobeValidate.questionImageUrl="" || obTobeValidate.questionImageUrl.match(regexp))
        {

            return true;
        }
        else{
            alert("please enter valid url of Question");
            return false;
        }

        if (obTobeValidate.solutionImageUrl="" || obTobeValidate.solutionImageUrl.match(regexp))
        {

            return true;
        }
        else{
            alert("please enter valid url for Solution..");
            return false;
        }

        if (obTobeValidate.videoUrl="" || obTobeValidate.videoUrl.match(regexp))
        {

            return true;
        }
        else{
            alert("please enter valid url for Video..");
            return false;
        }


            return true;


     },

并且在执行此代码后,如果我将questionImageUrl留空,则显示请输入有效的url,并且url验证在videoUrl等其他字段中不起作用。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

如果if语句使用赋值运算符(=)而不是比较符(==)。

此外,如果条件有效,请不要返回,因为您需要检查其他字段

validate: function (attrs) {

    var obTobeValidate = {};
    var regexp = /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/g;
    obTobeValidate.questionImageUrl = $("#addSolvedExampleContainer .solvedQuestionImage").val();
    obTobeValidate.solutionImageUrl = $("#addSolvedExampleContainer .solvedQuestionSolutionImage").val();
    obTobeValidate.videoUrl = $("#addSolvedExampleContainer .solvedQuestionVideoUrl").val();

    if (!attrs.title) {
        alert("please mention title..");
        return false;
    }

    if (!attrs.question) {
        alert("please enter the question");
        return false;
    }


    if (!attrs.solution) {
        alert("please enter the solution for the question..");
        return false;
    }

    if (obTobeValidate.questionImageUrl != "" && !obTobeValidate.questionImageUrl.match(regexp)) {
        return false;
    }

    if (obTobeValidate.solutionImageUrl != "" && !obTobeValidate.solutionImageUrl.match(regexp)) {
        alert("please enter valid url for Solution..");
        return false;
    }

    if (obTobeValidate.videoUrl != "" && !obTobeValidate.videoUrl.match(regexp)) {
        alert("please enter valid url for Video..");
        return false;
    }

    return true;
}