Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
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:
  
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, asp.net. 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="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:

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

Get the latest Asp.net, C#.net, 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

3 comments :

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 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.