9.2 C
Munich
Thursday, March 23, 2023

The solution to jquery check is null or empty

Must read

In this article, you going to see how to check null or empty value in a string using jquery. We gong to check in a detail.

Let’s start with the topic regarding the check of null or empty using jQuery.

The important point is how to achieve this topic.

It is very simple to complete this task easily and in a simple way.

We going to check null and empty by entering the value in input type text.

On click of button, we going to check entered in input type text is null or empty using jquery.

From below example you come to understand completely regarding check of null value.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Check null or empty value using jquery</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
	<div>
		Please Enter Text: <input type="text" id="txtName" />
		<input type="button" id="btnCheckVal" value="Please Check " />
		<div id="final_result"></div>
	</div>
	<script type="text/javascript">
	$(document).ready(function() {
		$('#btnCheckVal').click(function () {
			var txt = $('#txtName');
			$('#final_result').text('');
			if(txt.val() != null && txt.val() != '') {
				var res = 'You entered text :' + txt.val();
				$('#final_result').append("<div>");
				$('#final_result').append(res);
				$('#final_result').append("</div>");
			}
			else {
				var res = 'It is empty or null.<br/>Please Enter Valid Value..';
				$('#final_result').append("<div>");
				$('#final_result').append(res);
				$('#final_result').append("</div>");
			}
		});
	});
	</script>
</body>
</html>

Output to check null or empty value using jquery are as follow.

Output after entering value.
Output after entering value.

From the above output you come to understand that on click of submit button jquery is triggered.

When jquery is triggered the entered value show the result.

Now we going check the output, if null or not entered value in the input type text on click of button.

From below output you come to understand successfully.

Ouput when entered null value.
Output when entered null value.

From the above output you come to understand regarding check of null or empty value.

So finally done with the task of checking of empty value or a null value.

There are two ways you can check the empty or null value.

Below is first method, using this you can check with empty or null value.

First Method:

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

Second Method:

if ($('yourvariable').val().length != 0 && $('yourvariable').val() != '') {
// Your Code Here
}

Using above both method you come to understand check with null or empty value.

So finally we done with the task and you come to understand completely using both method.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Best Guide on jQuery UI DatePicker – Tips and Tricks

- Advertisement -spot_img

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest article