admin管理员组文章数量:1128486
What is the best way to test for an empty string with jquery-out-of-the-box, i.e. without plugins? I tried this.
But it did't work at least out-of-the-box. It would be nice to use something that's builtin.
I wouldn't like to repeat
if (a == null || a=='')
everywhere if some if (isempty(a))
would be available.
What is the best way to test for an empty string with jquery-out-of-the-box, i.e. without plugins? I tried this.
But it did't work at least out-of-the-box. It would be nice to use something that's builtin.
I wouldn't like to repeat
if (a == null || a=='')
everywhere if some if (isempty(a))
would be available.
- 1 possible duplicate of What is the best way to check for an empty string in JavaScript? – Adriano Carneiro Commented Oct 28, 2011 at 15:34
- Possible duplicate of How do you check for an empty string in JavaScript? – T.Todua Commented Dec 4, 2018 at 9:33
10 Answers
Reset to default 605if (!a) {
// is emtpy
}
To ignore white space for strings:
if (!a.trim()) {
// is empty or whitespace
}
If you need legacy support (IE8-) for trim()
, use $.trim
or a polyfill.
The link you gave seems to be attempting something different to the test you are trying to avoid repeating.
if (a == null || a=='')
tests if the string is an empty string or null. The article you linked to tests if the string consists entirely of whitespace (or is empty).
The test you described can be replaced by:
if (!a)
Because in javascript, an empty string, and null, both evaluate to false in a boolean context.
Based on David's answer I personally like to check the given object first if it is a string at all. Otherwise calling .trim()
on a not existing object would throw an exception:
function isEmpty(value) {
return typeof value == 'string' && !value.trim() || typeof value == 'undefined' || value === null;
}
Usage:
isEmpty(undefined); // true
isEmpty(null); // true
isEmpty(''); // true
isEmpty('foo'); // false
isEmpty(1); // false
isEmpty(0); // false
Check if data is a empty string (and ignore any white space) with jQuery:
function isBlank( data ) {
return ( $.trim(data).length == 0 );
}
if(!my_string){
// stuff
}
and
if(my_string !== "")
if you want to accept null but reject empty
EDIT: woops, forgot your condition is if it IS empty
Try executing this in your browser console or in a node.js repl.
var string = ' ';
string ? true : false;
//-> true
string = '';
string ? true : false;
//-> false
Therefore, a simple branching construct will suffice for the test.
if(string) {
// string is not empty
}
Since you can also input numbers as well as fixed type strings, the answer should actually be:
function isBlank(value) {
return $.trim(value);
}
Please see this code by jQuery:
if ($("#ItemId").val().trim() != "") {
console.log($("#ItemId").val());
}
Try this
if(!a || a.length === 0)
if((a.trim()=="")||(a=="")||(a==null))
{
//empty condition
}
else
{
//working condition
}
本文标签: javascriptWhat is the best way to test for an empty string with jqueryoutoftheboxStack Overflow
版权声明:本文标题:javascript - What is the best way to test for an empty string with jquery-out-of-the-box? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736719132a1949368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论