how to check if input is empty javascript?

Rate this post

how to check if input is empty javascript – Check whether an input text box is empty with JavaScript/jQuery. This article will discuss how to check whether an input text box is empty in JavaScript and jQuery.

how to check if input is empty javascript

Using JavaScript

$(document).ready(function() {
    $('#submit').click(function() {
        var value = document.getElementById('name').value;
        if (value === '') {
            alert('Enter your name');
        }
    })
});

javascript check if input is empty

if input value is null do something

if(document.getElementById("interview_no").value.length == 0)
{
    alert("empty")
}

Free Live Chat for Any Issue

Javascript function to check whether a field is empty or not

   function required(inputtx) 
   {
     if (inputtx.value.length == 0)
      { 
         alert("message");  	
         return false; 
      }  	
      return true; 
    } 

Don’t Miss : Check Array Empty Php

Read Also:  check Element Hidden in jQuery

How to Check If an Input Field is Empty Using jQuery?

Use the jQuery val() Method

<!DOCTYPE html>
<html lang="en">
<head>
<title>Check If Inputs are Empty using jQuery</title>
<style>
    .error{
        outline: 1px solid red;
    }    
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $('#userInp input[type="text"]').blur(function(){
        if(!$(this).val()){
            $(this).addClass("error");
        } else{
            $(this).removeClass("error");
        }
    });
});
</script>
</head>
<body>
    <form id="userInp">
        <p><label>First Name: <input type="text"></label></p>
        <p><label>Last Name: <input type="text"></label></p>
        <p><label>Email Address: <input type="text"></label></p>
    </form>
</body>
</html>

Download

I hope you get an idea about how to check if input is empty javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.