miyonfaga's avatar

Numbers with either comma or dot as separator : handle in front or back ?

Hi

My app is used by users that are either using comma or dot as a decimal separator.

The laravel backend is expecting a DOT as a number separator, because of "number" rule in form requests, and because of mysql.

The frontend is a Vue SPA app.

I've tried to change the validator rule so numbers are both accepted as "dot" or "comma" serapated, but then the DB complains about comma numbers !

So, I was wondering what should be the best practice : should the frontend ensure that numbers containing comma are replaced with dots before sending the SAVE call, or should it be handled on backend ?

0 likes
2 replies
hupp's avatar

@miyonfaga A common practice is to handle such conversions on the frontend before sending the data to the backend. This approach provides a good balance between user experience and data consistency.

Frontend Handling:

Implement logic on the frontend to convert the number format before sending it to the backend. Provide immediate feedback to users regarding any conversion-related issues. Backend Validation:

Continue to enforce the DOT as a number separator in Laravel form requests and validation rules to ensure consistency with the backend and database. By combining both approaches, you can maintain a consistent data format in the backend while ensuring a smooth and user-friendly experience on the frontend.

Please or to participate in this conversation.