I need to check to see if a var is null or has any empty spaces or for that matter just blank ("")
我需要检查一个var是否为空或者是否有任何空的空格或者只是空的("")
I have the following but not working:
我有以下几点但不工作:
var addr;
addr = " ";
if (!addr) {
// pull error
}
if I do following it works
如果我遵循它,它就会起作用
if (addr) {
}
What I need is something like this C# method:
我需要的是这样的c#方法:
String.IsNullOrWhiteSpace(value)
so that if it is null or has any spaces or no space, I can trap it
所以如果它是空的或者有任何空格或者没有空格,我可以设陷阱
6 个解决方案
#1
76
A non-jQuery solution that more closely mimics IsNullOrWhiteSpace
, but to detect null, empty or all-spaces only:
一种非jquery解决方案,更接近于模仿IsNullOrWhiteSpace,但只检测空、空或全空间:
function isEmptyOrSpaces(str){
return str === null || str.match(/^ *$/) !== null;
}
...then:
……然后:
var addr = ' ';
if(isEmptyOrSpaces(addr)){
// error
}
* EDIT * Please note that op specifically states "I need to check to see if a var is null or has any empty spaces or for that matter just blank". So while yes, "white space" encompasses more than null, spaces or blank my answer is intended to answer op's specific question. This is important because op may NOT want to catch things like tabs, for example.
* EDIT *请注意,op特别声明“我需要检查一个var是否为空或是否有任何空的空格,或者只是空的”。因此,虽然“白色空间”包含了多于空、空格或空白,但我的回答是为了回答op的特定问题。这很重要,因为op可能不希望捕获诸如制表符之类的内容。
#2
14
You can use if(addr && (addr = $.trim(addr)))
您可以使用if(addr && & (addr = $.trim(addr)))
This has the advantage of actually removing any outer whitespace from addr
instead of just ignoring it when performing the check.
这样做的好处是可以从addr中移除任何外部空白,而不是在执行检查时忽略它。
Reference: http://api.jquery.com/jQuery.trim/
参考:http://api.jquery.com/jQuery.trim/
#3
9
if (addr == null || addr.trim() === ''){
//...
}
A null
comparison will also catch undefined
. If you want false
to pass too, use !addr
. For backwards browser compatibility swap addr.trim()
for $.trim(addr)
.
空比较也会捕获未定义的值。如果你想要假也通过,使用!addr。为向后浏览器兼容性交换addr.trim()为$.trim(addr)。
#4
3
You can create your own method Equivalent to
您可以创建您自己的方法等效于
String.IsNullOrWhiteSpace(value)
String.IsNullOrWhiteSpace(值)
function IsNullOrWhiteSpace( value) {
if (value== null) return true;
return value.replace(/\s/g, '').length == 0;
}
#5
2
When checking for white space the c# method uses the Unicode standard. White space includes spaces, tabs, carriage returns and many other non-printing character codes. So you are better of using:
在检查空格时,c#方法使用Unicode标准。空白包括空格、制表符、回车符和许多其他非打印字符代码。所以你最好使用:
function isNullOrWhiteSpace(str){
return str == null || str.replace(/\s/g, '').length < 1;
}
#6
0
Simplified version of the above: (from here: https://*.com/a/32800728/47226)
上面的简化版本:(这里:https://*.com/a/32800728/47226)
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}
#1
76
A non-jQuery solution that more closely mimics IsNullOrWhiteSpace
, but to detect null, empty or all-spaces only:
一种非jquery解决方案,更接近于模仿IsNullOrWhiteSpace,但只检测空、空或全空间:
function isEmptyOrSpaces(str){
return str === null || str.match(/^ *$/) !== null;
}
...then:
……然后:
var addr = ' ';
if(isEmptyOrSpaces(addr)){
// error
}
* EDIT * Please note that op specifically states "I need to check to see if a var is null or has any empty spaces or for that matter just blank". So while yes, "white space" encompasses more than null, spaces or blank my answer is intended to answer op's specific question. This is important because op may NOT want to catch things like tabs, for example.
* EDIT *请注意,op特别声明“我需要检查一个var是否为空或是否有任何空的空格,或者只是空的”。因此,虽然“白色空间”包含了多于空、空格或空白,但我的回答是为了回答op的特定问题。这很重要,因为op可能不希望捕获诸如制表符之类的内容。
#2
14
You can use if(addr && (addr = $.trim(addr)))
您可以使用if(addr && & (addr = $.trim(addr)))
This has the advantage of actually removing any outer whitespace from addr
instead of just ignoring it when performing the check.
这样做的好处是可以从addr中移除任何外部空白,而不是在执行检查时忽略它。
Reference: http://api.jquery.com/jQuery.trim/
参考:http://api.jquery.com/jQuery.trim/
#3
9
if (addr == null || addr.trim() === ''){
//...
}
A null
comparison will also catch undefined
. If you want false
to pass too, use !addr
. For backwards browser compatibility swap addr.trim()
for $.trim(addr)
.
空比较也会捕获未定义的值。如果你想要假也通过,使用!addr。为向后浏览器兼容性交换addr.trim()为$.trim(addr)。
#4
3
You can create your own method Equivalent to
您可以创建您自己的方法等效于
String.IsNullOrWhiteSpace(value)
String.IsNullOrWhiteSpace(值)
function IsNullOrWhiteSpace( value) {
if (value== null) return true;
return value.replace(/\s/g, '').length == 0;
}
#5
2
When checking for white space the c# method uses the Unicode standard. White space includes spaces, tabs, carriage returns and many other non-printing character codes. So you are better of using:
在检查空格时,c#方法使用Unicode标准。空白包括空格、制表符、回车符和许多其他非打印字符代码。所以你最好使用:
function isNullOrWhiteSpace(str){
return str == null || str.replace(/\s/g, '').length < 1;
}
#6
0
Simplified version of the above: (from here: https://*.com/a/32800728/47226)
上面的简化版本:(这里:https://*.com/a/32800728/47226)
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}