jQuery验证插件:如何将错误视为警告并允许表单提交?

AsG*_*ets 9 validation jquery jquery-plugins jquery-validate

正如主题建议的那样,我正在使用bassistance.de验证插件(http://docs.jquery.com/Plugins/Validation/),即使存在验证错误,我也希望能够提交表单.基本上,我们只想使用插件来警告用户潜在的问题(请不要质疑可用性问题,我知道).

那么,有没有办法轻松做到这一点,还是应该侵入插件代码?

提前致谢!

whi*_*101 5

我遇到了类似的问题,我决定使用options.ignore属性. http://docs.jquery.com/Plugins/Validation/validate#toptions

$("form").validate({
  rules: {
    "hard": {word_count:10}, // No more than 10 words
    "soft_and_hard": {word_count:[30,10]} // No more than 40 words
  },
  ignore: ".error-okay"
});
Run Code Online (Sandbox Code Playgroud)

我使用自定义验证器来计算单词.我们决定在X字(他们要求的字数)之后显示错误,并且如果他们有X + T字则不允许表单提交.当它介于X和X + T之间时,我会在元素中添加一个类"error-okay"(其中".error-okay"是我作为ignore选项传递的内容).

jQuery.validator.addMethod("word_count", function(value, element, max) {
    var tolerance = 0;
    if (max instanceof Array){
        tolerance = max[1];
        max = max[0];
    }
    var typedWords = jQuery.trim(value).split(' ').length;

    if(typedWords <= max + tolerance) $(element).addClass("error-okay");
    else $(element).removeClass("error-okay");

    return (typedWords <= max);
}, function(max, ele){
    var tolerance = "";
    if (max instanceof Array){
        tolerance = "<br/>Definitly no more than " + ( max[0] + max[1] ) + " words.";
        max = max[0];
    }
    return "Please enter " + max +" words or fewer. You've entered " +
            jQuery.trim($(ele).val()).split(' ').length + " words." + tolerance;
});
Run Code Online (Sandbox Code Playgroud)

如果你在一个字段上有多个验证,你可能最好写一个invalidHandler函数,可能将它与我使用的添加类组合(例如:"word-count-okay").


And*_*ker 4

我不会质疑这方面的可用性问题:)。invalidHandler通过使用验证选项确实可以做到这一点:

$("#test-form").validate({
    invalidHandler: function() {
        /* Allow the user to see the error messages before submitting: */
        window.setTimeout(function() { 
            $("#test-form")[0].submit();
        }, 1000);
    }
});
Run Code Online (Sandbox Code Playgroud)

这是一个例子: http: //jsfiddle.net/ghDbd/