aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

Mar 25, 2013

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.

In previous articles I explained
jQuery get current page url and title, jQuery disable future dates in datepicker, jQuery Get Cursor position, jQuery Bind double click event to button control, jQuery create rounded corners for textbox and many articles relating to JavaScript, jQuery, Now I will explain how to check if string contains null or empty value using jQuery.

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="">
<title>jQuery Check if string empty or null</title>
<script type="text/javascript" src=""></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')
Enter Text: <input type="text" id="txtName" />
<input type="button" id="btnCheck" value="Check" />
Live Demo

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

Enter Text:

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Ganapathi said...

Hi Suresh Ji, it is allowing spaces..

Anonymous said...

Yes...its allowing spaces

Unknown said...

var a = $('#textbox1').val();
if (a.length == 0 || a.trim() == '' ) {
alert('text box is empty'); }
else {
alert('text box has value');

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.