Validation of float and not empty in JavaScript

Viewed 276

I am new to the world of web development and I am starting with basic applications. I am making a simple web page to add two numbers and trying to perform some basic validations that the input field can not be empty as well as the input must only be a float number.

My index.html:

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="styles.css">
    <link href="https://fonts.googleapis.com/css2?family=Merriweather&display=swap" rel="stylesheet">
    <title>Add Two Numbers</title>
</head>
<body>
    <form class="container center_div topSpacing">
        <h1 id="Title" class="text-center">Add Two Numbers</h1> 
        <div class="form-group">
        <label for="Num1">First Number</label>
        <input type="text" class="form-control" id="Num1" placeholder="Enter First Number">
        </div>
        <div class="form-group">
        <label for="Num2">Second Number</label>
        <input type="text" class="form-control" id="Num2" placeholder="Enter Second Number">
        </div>
        <div class="text-center">
            <button onclick="result()" type="button" class="btn btn-outline-success">Evaluate</button>
        </div>
    </form>
    <br>

    <h1 id="Title2" class="text-center"></h1>

    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <script type="text/javascript" src="index.js"></script>
</body>
</html>

My index.js:

let var1 = document.getElementById("Num1");
let var2 = document.getElementById("Num2");
function validate() {
    if(var1 == "" || var2 == "")
        return false;
    return true;
}

function buttonClick() {
    let sum = parseFloat(var1.value) + parseFloat(var2.value);
    return sum;
}

function result() {
    if(validate()) {
        document.getElementById("Title2").innerHTML = "Sum is: " + buttonClick();
    } else {
        window.alert("Wrong Inputs");
    }
}

The JavaScript code is not performing the validation as well as the summation.

2 Answers

Simple fix for solution is make the input fields to type number.

 <input type="number" class="form-control" id="Num1" placeholder="Enter First Number">
        </div>

 <input
          type="number"
          class="form-control"
          id="Num2"
          placeholder="Enter Second Number"
        />

change the validate function as below

 function validate() {
  const reg = /\-?\d+\.\d+/g;
  if (
    var1.value !== "" &&
    var2.value !== "" &&
    var1.value.match(reg) &&
    var2.value.match(reg)
  ) {
    return true;
  }
  return false;
}

Change this

function validate() {
if(var1 == "" || var2 == "")
    return false;
return true;

}

to

function validate() {
if(var1.value == "" || var2.value == "")
    return false;
else
    return true;
}

this should work.

Related