marioizquierdo / jquery.serializeJSON

Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.
MIT License
1.71k stars 433 forks source link

Remove extra spaces in serialized strings #95

Closed jonalxh closed 5 years ago

jonalxh commented 5 years ago

Hi, I wanna know if there is a way to remove extra spaces in the serialized strings, something like trim() which removes spaces of the beginning and the end.

It is just for not to have the need to iterate the json result again.

Thanks.

marioizquierdo commented 5 years ago

You could use the option parseWithFunction and define your own function to trim spaces. For example:

const trimSpaces = (val, inputName) => {
  if (typeof(val.trim) === 'function') { // check if is a string with the trim() method
    return val.trim();
  }
  return val;
}

$('form').serializeJSON({parseWithFunction: trimSpaces});
jonalxh commented 5 years ago

Thanks a lot, I'm glad there is a method of doing it. I'll try later.

jonalxh commented 5 years ago

It worked perfectly, thanks again. :+1: