0

I'm trying to change a value user enters to a true number (ex. 15 or 144.38). If it has unapproved characters (ex. $,) then they should be stripped. Also if user enters value with multiple periods (ex. 43.14.14) then I want to clear entire value. I was able to do step 1 but can't figure step 2, please advise. Here's my code so far http://jsbin.com/otawiVa/1

function myFunction()
{
  var str = document.getElementById("number_field").value;
  var res = str.replace(/[^0-9.}]/g,"");
  document.getElementById("approved_number").innerHTML=res;
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
Dmitry
  • 4,143
  • 10
  • 46
  • 57
  • 3
    For, the latter, you *could* just do `if (res.indexOf('.') !== res.lastIndexOf('.')) document.getElementById("number_field").value = ''` – Matt Nov 15 '13 at 15:26

2 Answers2

1

change your function to

function myFunction()
{
var str = document.getElementById("number_field").value;
var res = str.replace(/[^0-9.}]/g,"");
  if (res.indexOf('.') !== res.lastIndexOf('.')) {
    document.getElementById("approved_number").innerHTML="";
  } else {
   document.getElementById("approved_number").innerHTML=res;
  }
}

Example here:

http://jsbin.com/UNOHoYek/1

Sunil Verma
  • 2,490
  • 1
  • 14
  • 15
0

See http://jsbin.com/OKoJuloQ/3/ for a possible answer.

Changing user input without providing feedback is probably not a good idea.

Better to just handle what can be taken as a number and report the rest.

window.alert is just one way to report it.

The important bits are the conversion to Number and testing against NaN.

CODE (also available via link above)

<!DOCTYPE html>
<html>
<body>
<input type="text" value="" id="number_field">
<button onclick="myFunction()">Submit</button>
<p id="approved_number"></p>
<script>
  function myFunction() {
    try {
      var str = document.getElementById("number_field").value;
      // var res = str.replace(/[^0-9.}]/g,"");
      var res = str;
      if (Number.isNaN(Number(res))) {
        window.alert(res + " is not a number (NaN)\nOnly digits, decimal point,\nand exponent (e.g. 3.2e4) are allowed");
      } else {
        document.getElementById("approved_number").innerHTML=Number(res).toString(10);
      }
    } catch (exception) {
      window.alert(exception);
    }
  }
</script>
</body>
</html>
stackunderflow
  • 953
  • 7
  • 17
  • The exception handling is not needed at this point, but might be useful in case you get adventurous with further code changes. – stackunderflow Nov 15 '13 at 16:04
  • I have a requirement for my code to not produce any alerts. So if a user enters $15,144 - the app should just output a cleaned up value 15144 – Dmitry Nov 15 '13 at 16:08
  • Yeah, the alert was just an example. That's a comma in $15,144. You are not concerned about internationalization then? comma would be used as decimal point in German, where your use of the throusand mark would be a period in German (€15.144,00). I guess the exponent handling is not needed, but what if someone entered 1.5e4, that would just become 1.54 (shudder). – stackunderflow Nov 15 '13 at 16:21