0

I need to find a way of either inputting separator eg, 12,000 without getting NaN message and if this can't be done then showing a message instead. I have looked through various sites and StackOverflow and can't work out how to do this (newbeeee issue). Current code as follows:

<script language="JavaScript" type="text/javascript">
function calcsavings()

{

var B1=document.forms[0].B1.value;
var B2n = Number("1516");
var B2=+B2n + +B1;
var B4 = Number("0.138");

var t;
for (i=0; i<document.forms[0].ITR.options.length; i++)

{

if (document.forms[0].ITR.options[i].selected)
t = document.forms[0].ITR.options[i].value;
}

var result=(B1/t-(B2/(1+B4)))*t

result=Math.round(result);

document.getElementById("childcaresavings").innerHTML=result;

}

</script>

I have stripped out the non-working code I tried before posting.

1 Answers1

0

Is there anything wrong with adding a NaN check for result?

result=Math.round(result);
var htmlContent = Number.isNaN(result) ? "not a valid number" : result;
document.getElementById("childcaresavings").innerHTML=htmlContent ;

edit: If you're looking for a casual use of parsing. You can just use a global replace that will remove anything except numbers and periods.

if (document.forms[0].ITR.options[i].selected) {
  var inputVal = document.forms[0].ITR.options[i].value;
  var removedCommas = inputVal.replace(/[,]/g, "")
  if (removedCommas[0] === "£") {
    removedCommas = removedCommas.slice(1, removedCommas.length)
  }
  t = removedCommas
}

What this does: It takes your input and removes all commas and £ if it is the first character in the input. Assuming your user enters a sane entry, it will process the number as expected. If your use enters anything weird like / * & #, etc, NaN will be returned and your error will show.

Andrew
  • 7,201
  • 5
  • 25
  • 34
  • Andrew, Thanks, that worked absolutely perfectly and simply changed the message. Ideally I wanted to be able to use £ or , within the input box B1 but needed this isNaN solution because anything other than straight numbers with/without decimal point returned NaN. Thanks Andrew, this really helped. I don't even know if there is a workable solution to the , £ problem as it seems to be either too complex to implement or the solutions aren't working well. – maxballroom Oct 08 '17 at 14:35
  • @maxballroom Just to be clear, you're looking to be able to input £120,000. A number that has a comma that will still be able to be properly processed as a number? – Andrew Oct 08 '17 at 18:00
  • @maxballroom Didn't catch that part. It's not hard to do at all. But it depends on how strict you want it to be. Will you allow malformed combinations of commas and pounds like `1200,00`, `£120,000£`? Or will `£120,000` be the only thing you will enforce to be a real number? – Andrew Oct 08 '17 at 18:05
  • Hi Andrew, there's only one input box and nobody is going to put a number in probably greater than 90,000 but the other box (as you can see) is a income tax rate in the UK for tax payers of 20, 40 or 45%, The problem is people may put £12,000, 16,000 or whatever figure in and some may put a £ sign too. I just want to be able to get the thing to work so that it doesn't throw up an error,. I hope that makes sense. People will be using UK money with a comma separator and also maybe a decimal point (B1). It handles the point okay but not the £ sign or the comma separator. – maxballroom Oct 08 '17 at 18:56
  • Andrew, yes just either £120,000 or 120,000 - numbers like that. If they put the £ sign after the amount then it doesn't really matter if it works or not. So it doesn't have to be very strict. Yes and it has to be a real number with £ and or "," present. There may be two commas in if it reaches £1,000,000 however this is unlikely but you never know with some users! – maxballroom Oct 08 '17 at 18:59
  • @maxballroom updated. Some clarification needed on your end. – Andrew Oct 08 '17 at 19:33
  • Clarification, okay: User enters money number in box (B1) and selects (t) dropdown value for Tax Rate. A basic calculation is then made (in the script) which then yields a result. The result is causing the problem I think because when user inputs money number in the box (B1) and includes either a £ sign or comma separator (for thousands) then it caused the NaN problem which you fixed. Ideally need a way to allow entry of £ or separator comma in B1 so that it produces a result. Or if that can't be done, a way just to convert B1 commas and £ into a number. It works okay when just a number input. – maxballroom Oct 08 '17 at 21:00
  • @maxballroom done. give it a try and see if it's doing what you expect. Just keep in mind that in `var result=(B1/t-(B2/(1+B4)))*t` you have to use `parseInt(t)` in order to do proper math. `t` by itself is a string. – Andrew Oct 08 '17 at 21:29
  • Thanks. tried B1/t-(B2/(1+B4)))*ParseInt(t) but doesn't work (because it is probs in wrong place), It all works at present, it seems that it is just the B1 value inserted that is causing the problem because comma separator and £ can be inserted. Maybe I am looking at it too simply and without the expertise, – maxballroom Oct 08 '17 at 21:42
  • ParsInt(t) works when added earlier in the code (it worked without it), but naturally the B1 separator and currency problem still persists. – maxballroom Oct 08 '17 at 22:17