JavaScript中是否存在字符串.Empty,还是只是检查“”?
当前回答
function tell()
{
var pass = document.getElementById('pasword').value;
var plen = pass.length;
// Now you can check if your string is empty as like
if(plen==0)
{
alert('empty');
}
else
{
alert('you entered something');
}
}
<input type='text' id='pasword' />
这也是检查字段是否为空的通用方法。
其他回答
if ((str?.trim()?.length || 0) > 0) {
// str must not be any of:
// undefined
// null
// ""
// " " or just whitespace
}
或以函数形式:
const isNotNilOrWhitespace = input => (input?.trim()?.length || 0) > 0;
const isNilOrWhitespace = input => (input?.trim()?.length || 0) === 0;
最接近str.Empty(前提是str是String)的是:
if (!str.length) { ...
检查是否为空:
var str = "Hello World!";
if(str === ''){alert("THE string str is EMPTY");}
要检查它是否为string类型,请执行以下操作:
var str = "Hello World!";
if(typeof(str) === 'string'){alert("This is a String");}
尝试以下代码:
function isEmpty(strValue)
{
// Test whether strValue is empty
if (!strValue || strValue.trim() === "" ||
(strValue.trim()).length === 0) {
// Do something
}
}
试试这个
str.value.length == 0