You perform a test in each phase, but for several reasons.
You check when the user sets a value to provide immediate feedback to the user about whether the input is valid. This verification should only be used to improve the user experience. You can check while the user is typing, if necessary, but do not forget that the user enters an invalid partial input, as more may appear, and you do not want validation to interfere.
You check before the user submits the form to make sure that the submission is valid without resorting to the full cost of a full trip to the server. This will be mainly for things that were not or cannot be confirmed at the time of user input, and this may be due to some communication with the server to check if the username is available without reloading the page. This step also mainly relates to user benefit. Regardless of whether you check each item while recording or sending, it is up to you and should depend on what provides the best user interface and better matches the mental model of the user.
Finally, you need to check everything when it returns to the server, because you cannot trust the browser. This check is mainly for security. You can never assume that any data coming from your client is clean, because it may not be from your client. It may come from a hostile agent who imitates your client. Therefore, fully check everything, for all potential exploits and other unacceptable conditions, regardless of whether it was confirmed on the client.
Hope this helps.
source share