Validating salary input
As of now, if you press the Calculate Tax button, it takes the user to the results page from the front page. However, it does this regardless of input, so it will go to the next page even if there is no salary. The following validation checks must be done for it to be an acceptable value:
- Does it contain a value that is a number?
- Is the value above 0 (this rules out negatives)?
You might be wondering why we can’t just check whether the salary is above 0 as we have chosen a decimal keypad. There are two main reasons for this:
- The user can insert decimal points in a way that makes the input Not a Number (NaN) – for example,
4.5.6..
. - Even though the user cannot directly type text due to the keyboard being a decimal pad, they can still copy it from another application and paste it into our calculator, thus breaking the number-only
TextField
. You may think it’s worth just disabling pasting but it’s important...