Home > Web Front-end > JS Tutorial > body text

How to determine whether a string is empty in js

coldplay.xixi
Release: 2023-01-03 09:26:20
Original
44150 people have browsed it

JS method to determine whether a string is empty: 1. To determine whether a string is empty, the code is [if (string.length == 0)]; 2. To determine whether a string is an "empty" character That is, the user entered a space, and the code is [if (strings.replace(/(^s*)|(s*$)..].

How to determine whether a string is empty in js

this Tutorial operating environment: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer. This method is suitable for all brands of computers.

JS method to determine whether a string is empty:

Determine whether the string is empty

var strings = ''; 
if (string.length == 0) 
{ 
alert('不能为空'); 
}
Copy after login

Determine whether the string is an "empty" character, that is, the user has entered a space

var strings = ' '; 
if (strings.replace(/(^s*)|(s*$)/g, "").length ==0) 
{ 
alert('不能为空'); 
}
Copy after login

Determine whether the input string is empty or all characters Space

function isNull( str ){
if ( str == "" ) return true;
var regu = "^[ ]+$";
var re = new RegExp(regu);
return re.test(str);
}
Copy after login

If there is null, the above code cannot judge normally. The following code is the case when it is judged as null

var exp = null; 
if (exp == null) 
{ 
alert("is null"); 
}
Copy after login

When exp is undefined, the same result as null will be obtained, although Null and undefined are different.

Note: You can use this method when you want to judge null and undefined at the same time. The code is as follows

var exp = null; 
if (!exp) 
{ 
alert("is null"); 
}
Copy after login

If exp is undefined, or the number zero, or false, you will also get The same result as null, although null is different from the two. Note: This method can be used when you want to judge null, undefined, number zero, and false at the same time. The code is as follows

var exp = null; 
if (typeof exp == "null") 
{ 
alert("is null"); 
}
Copy after login

For backward compatibility, exp is null When, typeof null always returns object, so it cannot be judged this way.

<script type="text/javascript">
function testuser(){
var i= document.getElementByIdx_x("aa");
if (i.value=="null")
{
alert("请登录后再发表留言!")
return false;
}
else
{
alert(i.value)
return true;
}
}
</script>
Copy after login

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to determine whether a string is empty in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!