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.
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:
|
Method 2:
|
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
|
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. |
|||
|
|||
3 comments :
Hi Suresh Ji, it is allowing spaces..
Yes...its allowing spaces
var a = $('#textbox1').val();
if (a.length == 0 || a.trim() == '' ) {
alert('text box is empty'); }
else {
alert('text box has value');
}
Note: Only a member of this blog may post a comment.