0

I am using form.serialize() to get the list of parameters.

If parameter is empty or its space, i want to remove it from the list.

For example:

testAction.action?a=1&b=&c=3 

should give me

testAction.action?a=1&c=3

First I was using regex:

params = params.replace(/[^&]+=\.?(?:&|$)/g, '');

But problem is that if my url is

testAction.action?a=1&b=2&c= 

regex will return me

 testAction.action?a=1&b=2& (i have & at the end!)

After that I tried jQuery solution

$('.myForm').find('input, select').not("[value='']").serialize();

but this is only working for empty values -> if i have space parameter in the value that will pass.

Can you help me with some other solution?

Thanks

1
  • You should really handle the empty values at server-side, rather than change the way form data is passed. Commented May 13, 2013 at 16:33

2 Answers 2

1

Try

$(".myForm :input").filter(function () {return $.trim(this.value);}).serialize();
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this:

params = params.replace(/&?[^&?]+=(?=(?:&|$))/g, '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.