I have a form that uses jQuery to submit an ajax post and it serializes the form that is sent up. The code looks like this:
var form = $("form");
var action = form.attr("action");
var serializedForm = form.serialize();
$.post(action, serializedForm, function(data)
{
...
});
The problem here is that if a field has trailing white space, the serialize function will turn those spaces to plus (+) signs, when they should be stripped.
Is there a way to get the fields trimmed without doing the following:
$("#name").val( jQuery.trim( $("#name") ) );
You could try looping through the object and triming everything.
//Serialize form as array
var serializedForm = form.serializeArray();
//trim values
for(var i =0, len = serializedForm.length;i<len;i++){
serializedForm[i] = $.trim(serializedForm[i]);
}
//turn it into a string if you wish
serializedForm = $.param(serializedForm);
Trim all <input> and <textarea></textarea> element values in the DOM:
$('input, textarea').each(function(){
$(this).val(jQuery.trim($(this).val()));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With