jquery.validate plugin - how to trim values before form validation

前端 未结 16 579
野趣味
野趣味 2020-12-04 16:12

I\'m using the excellent jquery.validation plugin by Jörn Zaefferer and I was wondering whether there\'s a easy way to automatically trim form elements before they are valid

相关标签:
16条回答
  • 2020-12-04 16:59

    You can run code before the form is checked like this:

    var origCheckForm = $.validator.prototype.checkForm;
    $.validator.prototype.checkForm = function() {
        $(this.currentForm).find('.CodeMirror').each(function() {
            if(this.CodeMirror && this.CodeMirror.save) {
                this.CodeMirror.save();
            }
        });
    
        return origCheckForm.apply(this, arguments);
    };
    

    I've used it to save all my CodeMirror instances back to their corresponding texareas. You could use it to trim values instead if you want.

    0 讨论(0)
  • 2020-12-04 17:00

    Why not do this?

    Validation is occurring after the keyup event. On keyup replace textbox value with its trimmed value (or use a regex to remove any space):

    $("#user_name").on("keyup", function(){
        $("#user_name").val($.trim($("#user_name").val()));
    });
    
    0 讨论(0)
  • 2020-12-04 17:03

    Since I want this behavior on all my forms by default I decided to modify the jquery.validate.js file. I applied the following change to onfocusout method:

    Original:

    onfocusout: function (element, event) {
        if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) {
            this.element(element);
        }
    }
    

    To:

    onfocusout: function (element, event) {
        if (element.tagName === "TEXTAREA" || (element.tagName === "INPUT" && element.type !== "password")) {
            element.value = $.trim(element.value);
        }
        if (!this.checkable(element) && (element.name in this.submitted || !this.optional(element))) {
            this.element(element);
        }
    }
    

    I do want to allow spaces at the begging and end of password.

    autoTrim could be added as a property to options.

    0 讨论(0)
  • 2020-12-04 17:03

    I like the approach by xuser (https://stackoverflow.com/a/10406573/80002), however, I do not like messing with the plugin source code.

    So, I suggest doing this instead:

     function injectTrim(handler) {
      return function (element, event) {
        if (element.tagName === "TEXTAREA" || (element.tagName === "INPUT" 
                                           && element.type !== "password")) {
          element.value = $.trim(element.value);
        }
        return handler.call(this, element, event);
      };
     }
    
    
     $("form").validate({
        onfocusout: injectTrim($.validator.defaults.onfocusout)
     });
    
    0 讨论(0)
  • 2020-12-04 17:05

    When downloading validator.js, there is a file called additional-methods.js that contains the method "nowhitespace" and "lettersonly" that will strip out any white space in a field.

    rules: {
      user_name: {
        required: true,
        minlength: 3,
        nowhitespace: true
      }
    }
    
    0 讨论(0)
  • 2020-12-04 17:09

    Pulled the regex from the jquery validator. Just override their email validation.

    $.validator.addMethod("email", function(value, element) {
    value = value.trim();
    return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
    }, "Please enter a valid email.");
    
    0 讨论(0)
提交回复
热议问题