How to check whether the input text field contains only white spaces?

后端 未结 3 608
感情败类
感情败类 2021-01-04 12:18

What is the easiest way to check in Javascript whether the input text field is empty (contains nothing or white spaces only)?

3条回答
  •  一向
    一向 (楼主)
    2021-01-04 12:42

    var str = document.getElementById("myInput").value;
    if (str.match(/^\s*$/)) {
        // nothing, or nothing but whitespace
    } else {
        // something
    }
    

提交回复
热议问题