Take the 2-minute tour ×

I'm trying to check if a form input has any value (doesn't matter what the value is) so that I can append the value to the action URL on submit if it does exist. I need to add the name of the param before adding the value, and just leaving a blank param name like "P=" without any value messes up the page.

Here's my code:

function getParam() {

// reset url in case there were any previous params inputted

    document.form.action = 'http://www.domain.com'

    if (document.getElementById('p').value == 1) {
        document.form.action += 'P=' + document.getElementById('p').value;

    if (document.getElementbyId('q').value == 1) {
        document.form.action += 'Q=' + document.getElementById('q').value;


and the form:

<form name="form" id="form" method="post" action="">
    <input type="text" id="p" value="">
    <input type="text" id="q" value="">
    <input type="submit" value="Update" onClick="getParam();">

I thought setting value == 1 would do a simple exists, doesn't exist check regardless of what the submitted value was, but I guess I'm wrong.

Also, I'm using if statements, but I believe that's bad code, since I don't have an else. Perhaps, using a switch statement, though I'm not sure how I would set that up. Perhaps:

switch(value) {
    case document.getElementById('p').value == 1 :
    case document.getElementById('q').value == 1 :
        document.form.action += 'Q=' + document.getElementById('q').value; break;
share|improve this question
You won't be submitting anything if your input fields do not have names. –  kennebec Apr 6 '10 at 20:59
true, but in this case the form is only there to update the url and since I'm grabbing the values of those inputs by id, it's not really necessary. –  Choy Apr 6 '10 at 21:07
You do realize you can change your method to GET, and the values will be appended to the querystring? You should be able to handle empty values server-side without problems; what do you mean messes up the page? –  RedFilter Apr 6 '10 at 21:12
Do it RESTfully stackoverflow.com/questions/671118/…. –  N 1.1 Apr 6 '10 at 21:16
add comment

1 Answer

up vote 7 down vote accepted
var val = document.getElementById('p').value;
if (/^\s*$/.test(val)){
   //value is either empty or contains whitespace characters
   //do not append the value
   //code for appending the value to url

P.S.: Its better than checking against value.length because ' '.length = 3.

share|improve this answer
This seems to work opposite. When I leave the field blank it appends P= with no value (since it's blank) and when I type something in, it doesn't append anything. –  Choy Apr 6 '10 at 21:18
:) you are supposed to append when if clause evaluates to false –  N 1.1 Apr 6 '10 at 21:22
Why not just invert the if, since one branch will be empty?, e.g. if (!/^\s*$/.test(val)) { //... –  CMS Apr 6 '10 at 21:30
ahh, lol. I misunderstood what you were testing for. I thought you were testing for any character (which is basically what CMS is saying) vs empty or whitespace characters (which you clearly stated, doh!). thanks a lot. And just as a follow up question, is it bad practice to have if statements with no else? –  Choy Apr 6 '10 at 21:31
@CMS: The above code is just for explaining purpose. :) –  N 1.1 Apr 6 '10 at 21:35
show 1 more comment

Your Answer


