jQuery Check Empty Value or Null Value | Check if String Contains Null or Empty Value


jQuery Check Empty Value or Null Value | Check if String Contains Null or Empty Value

Mar 25, 2013
Introduction

Here I will explain how to use jQuery to check null or empty value or check if string contains null or empty value using jQuery.

Description
  
I
If you want to check if string empty or null for that check below methods

Method 1:

if ($('yourvariable').val() != null && $('yourvariable').val() != '') {
// Your Code Here
}
Method 2:

if ($('yourvariable').val().length != 0 && $('yourvariable').val() != '') {
// Your Code Here
}
If you want to see it in complete example check below code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Check if string empty or null</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#btnCheck').click(function () {
var txt = $('#txtName');
if (txt.val() != null && txt.val() != '') {
alert('you entered text ' + txt.val())
}
else {
alert('Please enter text')
}
})
});
</script>
</head>
<body>
<div>
Enter Text: <input type="text" id="txtName" />
<input type="button" id="btnCheck" value="Check" />
</div>
</body>
</html>
Live Demo

To check for live demo click on below button without entering text and after enter text

Enter Text:  

Comments