'How can I submit just filled inputs of a form in ASP.NET Core?

I like to know if it possible to submit just filled inputs of a form and show their name and value in url in browser.

Example:

<form method="get">
   <input name="firstname" value="a" />
   <input name="lastname" value="" />
</form>

I like to show in url as:

?firstname=a

not as:

?firstname=a&lastname=

Thanks.



Solution 1:[1]

You'll have to use javascript to achieve this result. For example you can make empty inputs disabled so browser doesn't send their values

Form

<form method="get" id="the-form">
    <input name="firstname" value="a" />
    <input name="lastname" value="" />

    <button type="submit">Submit</button>
</form>

Javascript

<script>
    let form = document.querySelector('#the-form');
    form.addEventListener('submit', () => {
        let inputs = form.querySelectorAll('input');
        for (let input of inputs) {
            if (!input.value) {
                input.disabled = true;
            }
        }
    })
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexander