Found 6685 Articles for Javascript

How to check input file is empty or not using JavaScript/jQuery?

Rushi Javiya
Updated on 10-Mar-2023 16:52:40

7K+ Views

In JavaScript, while working with the form elements, we need to validate the input fields and form elements when a user enters the value. In this tutorial, we will work with the file input. We will also learn to validate the file input. Sometimes, we may be required to check if the file is selected in the input field, then only enable the submit button; otherwise, disable the submit button. So, it will not allow users to submit the form or file without selecting it. Validate the file input using JavaScript In JavaScript, we can access the file input ... Read More

How to check if the clicked element is a div or not in JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:51:11

8K+ Views

Have you ever worked with the modal or seen it on any website? When you click outside the modal, it closes the modal. We must detect whether users have clicked on or outside the modal element in such cases. If users click outside the modal element, we need to close the modal element. In this tutorial, we will learn different ways to detect the click on the div element. Use the onClick attribute to check if the clicked element is a div In HTML, we can add the onClick attribute to any HTML element and assign it the ... Read More

How to check if the string contains only digits in JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:48:05

6K+ Views

We may require to find the string, which contains only digits but not any other characters while developing. The easiest way to check can parse the numbers from the string and compare its length with the original string’s length. If both are the same, that means the string contains only digits. Users can use the parseInt() method to parse the numbers from the string. However, in this tutorial, we will learn other methods to check if the string contains only digits in JavaScript. Use the for-loop and CharCodeAt() method We can iterate through the string using the for-loop. ... Read More

How to check if one date is between two dates in JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:44:52

1K+ Views

In JavaScript, we can use the Date() object to create different timestamps. Also, we may be required to check if one date is between two using JavaScript. For example, we want to create a filter for orders in the eCommerce application based on the dates. So, we should be able to filter all orders between the two dates that users enter into the date input field. Another real-world use case of checking one date between two is in the banking application. For example, while developing the banking system application, developers need to create a filter allowing users to ... Read More

How to check if the date is less than 1 hour ago using JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:42:07

3K+ Views

In this tutorial, we will learn to find the difference between two dates and check if the difference is less than one hour. Sometimes, developers must play with the Date object and perform some operation every hour. So, this can be one approach that checks if a particular operation is performed before an hour, perform it again; otherwise, wait to complete the hour. Here, we will learn different approaches to check if the date is less than 1 hour ago using JavaScript. Use the getTime() method The getTime() method returns the total milliseconds for the date from 1st ... Read More

How to check if a string is html or not using JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:38:57

6K+ Views

Sometimes, developers require to manage the HTML from JavaScript. For example, developers need to append some HTML nodes to a particular HTML element by accessing them in JavaScript. So, before we append an HTML string to any HTML element using JavaScript, we need to evaluate the string we are appending and check if it is valid. If we append the HTML string, which has an opening tab but doesn’t contain the closing tag, it can generate errors in the webpage. So, we will learn different approaches to validate the HTML string using JavaScript. Use the regular expression ... Read More

How to check if a number evaluates to Infinity using JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:36:52

393 Views

In JavaScript, when we divide any number by zero, we can get the infinity value. Also, developers can make a mistake in writing the mathematical expression which evaluates to Infinity. So, before we perform any operation with the returned value from the mathematical expression, we need to check if the number value is finite. Here, we will learn three approaches to check if a number evaluates to Infinity using JavaScript. Comparing the number value with the Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY In JavaScript, a Number is an object containing different properties and methods related to numbers. The POSITIVE_INFINITY and ... Read More

How to check whether or not a browser tab is currently active using JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:34:32

17K+ Views

Users can open multiple tabs in the browser, and they can also check if any particular tab is currently active or not in the browser. For example, if you have given a proctored test, you can observe that it detects that tab is changed whenever you change the tab in the browser So, there can be many applications and uses for checking whether the browser tab is active. This tutorial will teach us two methods to check whether the browser’s tab is active. Using the onblur() and onfocus() methods of the window object The window object contains the different ... Read More

How to check for two timestamps for the same day in JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:32:05

5K+ Views

It never happens that you use JavaScript in developing your application, and you don’t use the Date object. The Date object is very important in JavaScript, which allows us to create and manipulate the date according to the developer’s requirements. In this tutorial, we will learn to check whether two timestamps are for the same or different days. In real-time development, it is very useful. For example, we wanted users to perform some everyday tasks. So, we need to check whether users have performed the task for today, and we can check that by comparing the last date of ... Read More

How to check if an object is empty using JavaScript?

Rushi Javiya
Updated on 10-Mar-2023 16:30:55

3K+ Views

In JavaScript, the object is the most important data type, and we require its most of the time while developing the application with the JavaScript framework. Sometimes, we need to check whether an object is empty and perform the operation based on the object value. For example, you are fetching the data from the database; if it isn’t found, you can get an empty object. When you perform some operation or execute some method on an empty object, it raises an error in the program. So, it’s better first to check whether the object is empty. We will ... Read More

Advertisements