How to check if form input has value

前端 未结 1 393
既然无缘
既然无缘 2021-01-05 12:42

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 t

相关标签:
1条回答
  • 2021-01-05 12:52
    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 讨论(0)
提交回复
热议问题