Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
How to check whether i submit blank input in javascript? i have written a program but it is not working

I have written a javascript code in an HTML file using a script tag, where I want to know whether my input is greater than or less than 10 or equals to 10 or any blank input. I use the if-else method to solve this problem, but my last else if condition which is "Your input is blank" not outputting in any way.

//f1 function will run when some hit the submit button
function f1() {
  //here we are taking the value of the input field using id "myinput" in the variable called myinputvar
  var myinputvar = document.getElementById("myinput").value;
  if (myinputvar > 10) {
    document.getElementById("txt").innerHTML = "Your input number is greater than 10";
  } else if (myinputvar < 10) {
    document.getElementById("txt").innerHTML = "Your input number is less than 10";
  } else if (myinputvar = 10) {
    document.getElementById("txt").innerHTML = "Your input number is 10";
  } else if (myinputvar = " ") {
    document.getElementById("txt").innerHTML = "Your input is blank";
  }
}
<!-- this is the input tag to take inputs from user -->
Enter a integer number between 0 to anynumber = <input type="text" id="myinput"><br><br>
<!-- this is the button to submit the value -->
<button onclick="f1()">submit</button>
<!-- this is the heading tag to print the output -->
<h1 id="txt"></h1>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

A general way to check for only whitespace input uses the regex pattern ^\s*$. Sample code:

if (/^\s*$/.test(inputvar)) {
    document.getElementById("txt").innerHTML = "Your input is blank";
}
about 4 years ago · Juan Pablo Isaza Report

0

Wrap in a form and make it required

Also cast to number

document.getElementById("myForm").addEventListener("submit", function(e) {
  e.preventDefault(); // stop submission
  //here we are taking the value of the input field using id "myinput" in the variable called myinputvar
  var myinputvar = +document.getElementById("myinput").value; // make it a number
  if (myinputvar > 10) {
    document.getElementById("txt").innerHTML = "Your input number is greater than 10";
  } else if (myinputvar < 10) {
    document.getElementById("txt").innerHTML = "Your input number is less than 10";
  } else if (myinputvar = 10) {
    document.getElementById("txt").innerHTML = "Your input number is 10";
  } else if (myinputvar = " ") {
    document.getElementById("txt").innerHTML = "Your input is blank";
  }
})
<form id="myForm">
  Enter a integer number between 0 to anynumber =
  <input type="text" required id="myinput"><br><br>
  <button>submit</button>
</form>
<h1 id="txt"></h1>

about 4 years ago · Juan Pablo Isaza Report

0

pay attention not to use the shortcut:

if (inputvar) {
    // this won't catch "0"!
}

but the more proper

if (inputvar === "") {
    // is empty
}

and of course you should check for this condition first, if you want to handle numbers as input.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!