• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Check if Input is Empty Using jQuery

Check if Input is Empty Using jQuery

January 22, 2022 Leave a Comment

We can use jQuery to check if an input value is empty by using the val() method and the JavaScript length property. We simply get the value of the input using the val() method, and then check to see if its length is greater than 0.

if ($("input").val().length > 0) {
  // input is NOT empty
} else {
  // input IS empty
}

If you are using WordPress, don’t forget to change the $ to jQuery as below:

if (jQuery("input").val().length > 0) {
  // input is Not empty
} else {
  // input is empty
}

An example of using jQuery to check if an input is empty on a form

Below we will have a simple form with a name field and a submit button. One thing that is common in a lot of online forms is making sure the input fields are filled out before allowing the user to submit the form.

So below we will have a form that only lets you submit the information if you enter in a name in the name input box. Here is the HTML code:

<form>
  <label for="fname">Full Name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

We will use the jQuery val() method to get the value of the input and then use the JavaScript length property to see if the input is empty or not.

We will finally use jQuery to disable or enable the submit button based on whether a name exist or not. The submit button will start as disabled, and once someone starts typing a name, we will enable the button. We will do this using the jQuery keyup() method.

Here is the code:

$('#fname').keyup(function(){
  if ($("#fname").val().length > 0) {
    // input is Not empty, enable button
    $('button').prop('disabled', false);
  } else {
    // input is empty, disable button
    $('button').prop('disabled', true);
  }
});

The final code and output for this example of checking if an input value is empty is below:

Code Output:



Full Code:

<form>
  <label for="fname">Full Name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit" disabled>Submit</button>
</form>

<script>

$('#fname').keyup(function(){
  if ($("#fname").val().length > 0) {
    // input is Not empty, enable button
    $('button').prop('disabled', false);
  } else {
    // input is empty, disable button
    $('button').prop('disabled', true);
  }
});

</script>

Other Articles You'll Also Like:

  • 1.  Using jQuery to Check if Element Has Class
  • 2.  jQuery has – Check if an Element Has Other Elements
  • 3.  Using jQuery to Delete an Element
  • 4.  Using jQuery to Get the Top Position of Element
  • 5.  Using jQuery to Empty the Contents of an HTML Element
  • 6.  jQuery contains – How to Check if a Paragraph Contains Specific Text
  • 7.  jQuery before – Insert HTML Before Another Element
  • 8.  How to Uncheck All Checkboxes in jQuery
  • 9.  Using jQuery to Change Inner HTML
  • 10.  Using jQuery to get Textarea Value

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy