I am implementing a business application and wounder what's the best way to validate forms, and when.
An example is a form where three textfields where a currency amount is expected. A valid input is of the form
d is a digit and
D is one or more digits.
When to validate?
When the user is typing (e.g. validate the input after every char is typed). A problem is if the input needs to be in an invalid state before it gets valid.
When the focus of the textfield is lost.
When the users press the submit button.
How to validate?
Prevent "invalid" characters beeing typed e.g. letters in a number or a currency field.
Show an error indication (e.g. red border or show the input value in red text-color).
Prevent the user moving focus (it's important that the default value is valid in this case) and show error indication.
My current solution is to have a valid default value e.g.
0.00 for currency and
1 for quantity. And prevent letters beeing typed in currency and number fields. Sometimes the input needs to be in a invalid state, so I only validate when the focus is leaving the field. If the value is invalid, I keep the focus and show an indication. An empty field will be filled with the default value. But I don't know if this is good usability practice.