重新构建jQuery表单验证

时间:2012-11-15 17:54:23

标签: javascript jquery jquery-plugins

我正在尝试通过(http://jorenrapini.com/blog/javascript/the-simple-quick-and-small-jquery-html-form-validation-solution)重新制作jQuery脚本。此脚本正在检查是否填充了from,如果没有,则会显示错误消息。

我想要做的是仅在填写两个表单输入字段中的一个时获取错误消息,如果它们都不是,则应忽略它们。表单字段名为“firstinput”和“secondinput”(您可以在代码中看到它们的ID)。

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["firstinput", "secondinput"];
    // If using an ID other than #email or #error then replace it here
    email = $("#email");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";

    emailerror = "Please enter a valid e-mail.";

    $("#theform").submit(function(){    
        //Validate required fields
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }

        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function(){       
       if ($(this).hasClass("needsfilled") ) {
            $(this).val("");
            $(this).removeClass("needsfilled");
       }
    });
});

有人可以帮我解决一下,我真的很感激。 /一个花了很多时间解决这个问题而没有运气的女孩:(

1 个答案:

答案 0 :(得分:1)

我会将for循环包装在一个条件中,以评估一个或另一个是否有值。

if($("#field1").val() == "" && $("#field2").val() == ""){
//Ignore
}else{
//Do something
}

$(document).ready(function(){
    // Place ID's of all required fields here.
    required = ["firstinput", "secondinput"];
    // If using an ID other than #email or #error then replace it here
    email = $("#email");
    errornotice = $("#error");
    // The text to show up within a field when it is incorrect
    emptyerror = "Please fill out this field.";

    emailerror = "Please enter a valid e-mail.";

    $("#theform").submit(function(){    
        //Validate required fields
      if($("#firstinput").val() != "" || $("#secondinput").val() != "")
      {
        for (i=0;i<required.length;i++) {
            var input = $('#'+required[i]);
            if ((input.val() == "") || (input.val() == emptyerror)) {
                input.addClass("needsfilled");
                input.val(emptyerror);
                errornotice.fadeIn(750);
            } else {
                input.removeClass("needsfilled");
            }
        }
      }
        // Validate the e-mail.
        if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
            email.addClass("needsfilled");
            email.val(emailerror);
        }

        //if any inputs on the page have the class 'needsfilled' the form will not submit
        if ($(":input").hasClass("needsfilled")) {
            return false;
        } else {
            errornotice.hide();
            return true;
        }
    });

    // Clears any fields in the form when the user clicks on them
    $(":input").focus(function(){       
       if ($(this).hasClass("needsfilled") ) {
            $(this).val("");
            $(this).removeClass("needsfilled");
       }
    });
});