如何在javascript中验证非必需字段之前对其进行修剪



如何在验证下面代码中的非必需字段之前修剪业务名称。正在使用jquery-validation@1.17.0版本来验证表单。对于必填字段,我可以修剪,但我不知道如何修剪非必填字段。提前感谢

form.validate({
rules: {
firstName: {
required: {
depends:function(){
jQuery(this).val(jQuery.trim(jQuery(this).val()));
return true;
}	
},

},
businessName: {
onewordonly: ['1'],
maxlength: 15,
lettersonly: true
},
					      
},
messages: {
firstName: {
required: "Please enter your first name",
},
businessName: {
onewordonly: "bla bla",
maxlength: "bla bla",
lettersonly: "bla bla"
},

},
});

您可以使用depends在其他参数中执行trim,如接受布尔值作为值的lettersonly

businessName: {
maxlength: 15,
lettersonly: {
depends: function() {
$(this).val($.trim($(this).val()));
return true;
}
}
}

相关内容

  • 没有找到相关文章