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

前端 未结 16 667
野趣味
野趣味 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 17:15

    Add a new jQuery validator method requiredNotBlank. Then apply that function to your elements rather than the default required function. This solution doesn't change the original validator source code, nor does it modify the default required function, nor does it modify the element's value directly.

    // jQuery Validator method for required not blank.
    $.validator.addMethod('requiredNotBlank', function(value, element) {
        return $.validator.methods.required.call(this, $.trim(value), element);
    }, $.validator.messages.required);
    
    // ...
    $('#requiredNotBlankField').rules('add', 'requiredNotBlank');
    

提交回复
热议问题