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
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');