• 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 / JavaScript / How to Use JavaScript to Check if a String is Empty

How to Use JavaScript to Check if a String is Empty

January 31, 2022 Leave a Comment

We can check if a string is empty by using an if else conditional statement, and then use JavaScript to do something with this information. This is our preferred method to use.

var someString = "";
if (someString) {
  // The string is NOT empty
} else {
  // The string IS empty
}

You can also check if a string is empty by using the === operator and compare it to an empty string.

var someString = "";
if (someString === "") {
  // The string IS empty
} else {
  // The string is NOT empty
}

An example of using JavaScript to check if the string in 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" onkeyup="checkString()">
  <button id="button1" type="submit" value="Submit" disabled>Submit</button>
</form>

We will use the value property along with the getElementById method to get the value of the input. We will then have an if else statement to check if the input value is empty or not. If the value is not empty, we will enable the button to be clicked.

We will finally use JavaScript 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 with the help of the onkeyup event.

Here is the code:

function checkString(){
  if (document.getElementById("fname").value) {
    // input is Not empty, enable button
    document.getElementById("button1").disabled = false;
  } else {
    // input is empty, disable button
    document.getElementById("button1").disabled = true;
  }
};

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

Code Output:




Full Code:

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

<script>

function checkString(){
  if (document.getElementById("fname").value) {
    // input is Not empty, enable button
    document.getElementById("button1").disabled = false;
  } else {
    // input is empty, disable button
    document.getElementById("button1").disabled = true;
  }
};

</script>

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get Today’s Date
  • 2.  Using JavaScript to Add to Array
  • 3.  Convert String to Array in JavaScript
  • 4.  Using JavaScript to Calculate Average of Array of Numbers
  • 5.  Using JavaScript to Remove the Last Character From a String
  • 6.  JavaScript Random Number – How to Generate a Random Number In JavaScript
  • 7.  Using Javascript to Check if Value is Not Undefined
  • 8.  JavaScript slice – How to Get a Substring From a String
  • 9.  Remove Empty Strings from an Array in JavaScript
  • 10.  charAt() JavaScript – Getting the Index Position of a Character

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