1

How to strip all whitespaces (like spaces, tabs, newlines) from jQuery Ajax Data?

data: $('#field1, #field2, #field3').serialize() 
2
  • These questions may help you serialize and replaceAll Commented Feb 13, 2014 at 12:23
  • @Xeon: Already read them. Commented Feb 13, 2014 at 12:35

1 Answer 1

2

Try something like

var array = $('#field1, #field2, #field3').serializeArray(); $.each(array, function(i,o){ o.value = o.value.replace(/\s/g, ''); }) .... data: array 

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! Doesn't \s cover all whitespaces?
Thanks again! Unfortunately I can't vote up your answer yet as it requires 15 reputation.
Additional question: Is it possible to do it all in one line?
not as far as I know... if the inputs are not properly sanitized this is the only safe way I'm seeing
@simplethings another solution you can try is data: $('#field1, #field2, #field3').serialize().replace(/\s/g, '')
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.