mardi 14 juin 2016

Addition Calculator in Javascript not working

I have an addition calculator that checks with an if statement whether the inputs are numbers. However, when I enter non-numeric characters, the alert does not show. Here is my javascript code.

function addition() {
  var a = document.getElementById("1st").value;
  var b = document.getElementById("2nd").value;
  var check = /^[0-9]*$/;
  if (!(a.search(check))) {
    window.alert("Please enter only numbers!");
  }     
  var c = Number(a) + Number(b);
  document.getElementById("answer").innerHTML = c;

}

Aucun commentaire:

Enregistrer un commentaire