• 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
  • VBA
  • 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 Round to 1 Decimal
  • 2.  JavaScript Check if Attribute Exists in HTML Element
  • 3.  JavaScript atanh – Find Hyperbolic Arctangent of Number
  • 4.  Using JavaScript to Add Leading Zeros
  • 5.  Using JavaScript to Count Even Numbers in an Array
  • 6.  How to Convert a String to Lowercase In JavaScript
  • 7.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 8.  Add Days to a Date Using JavaScript
  • 9.  Using JavaScript to Change the Image src
  • 10.  JavaScript tanh – Find Hyperbolic Tangent of Number Using Math.tanh()

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy