最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if form input has value - Stack Overflow

programmeradmin8浏览0评论

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 = ''

    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();">
</form>

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 :
        document.form.action += 'P=' + document.getElementById('p').value; :
    case document.getElementById('q').value == 1 :
        document.form.action += 'Q=' + document.getElementById('q').value; break;
}

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();">
</form>

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 :
        document.form.action += 'P=' + document.getElementById('p').value; :
    case document.getElementById('q').value == 1 :
        document.form.action += 'Q=' + document.getElementById('q').value; break;
}
Share Improve this question edited Feb 6, 2012 at 0:55 Hubert Kario 22.8k4 gold badges28 silver badges45 bronze badges asked Apr 6, 2010 at 20:52 ChoyChoy 2,11711 gold badges39 silver badges49 bronze badges 4
  • You won't be submitting anything if your input fields do not have names. – kennebec Commented Apr 6, 2010 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 Commented Apr 6, 2010 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? – D'Arcy Rittich Commented Apr 6, 2010 at 21:12
  • Do it RESTfully stackoverflow.com/questions/671118/…. – N 1.1 Commented Apr 6, 2010 at 21:16
Add a comment  | 

1 Answer 1

Reset to default 13
var val = document.getElementById('p').value;
if (/^\s*$/.test(val)){
   //value is either empty or contains whitespace characters
   //do not append the value
}
else{
   //code for appending the value to url
}

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

发布评论

评论列表(0)

  1. 暂无评论