There is no better way, but rather general lines.
A alert()
allows you to send an error window to the user, but has the problem of blocking the entire browser in the process - which could detract from the experience.
To help your user, you can:
- Display a tooltip in your input field warning the user that the value entered is invalid;
- Use standard tools to your advantage: When you build a form within a
<form>
tag, your browser helps you validate the form. Fields of type email
, number
, or with attributes such as required
and maxlength
automatically block <submit>
of the form until all the data is as requested.
- Do not just leave the form validation on the front end, check the desired types on the back end, and return an error with Internal Server Error code 500 when an invalid input is identified.
For example, to send an error using Express:
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Another option: you can indicate in the error response which fields are invalid:
res.status(500).send( JSON.stringify( { invalidFields: ['nome','endereço'] } ));
And on your front end display a stripe with the information:
"The following fields are invalid formatting: name, address"