'IsNullOrWhitespace' in JavaScript?

后端 未结 8 1030
-上瘾入骨i
-上瘾入骨i 2020-12-02 22:22

Is there a JavaScript equivalent to .NET\'s String.IsNullOrWhitespace so that I can check if a textbox on the client-side has any visible text in it?

I\'d rather do

8条回答
  •  情歌与酒
    2020-12-02 22:44

    Try this out

    /**
      * Checks the string if undefined, null, not typeof string, empty or space(s)
      * @param {any} str string to be evaluated
      * @returns {boolean} the evaluated result
    */
    function isStringNullOrWhiteSpace(str) {
        return str === undefined || str === null
                                 || typeof str !== 'string'
                                 || str.match(/^ *$/) !== null;
    }
    

    You can use it like this

    isStringNullOrWhiteSpace('Your String');
    

提交回复
热议问题