If you have a custom login page, you can use the Auth widgets and create a custom sign-in nanoflow.
In this nanoflow you can trim the password before using it to perform the login action.
I wouldn't recommend automatically trimming unless you explicitly exclude spaces from passwords, otherwise it'll be impossible for other users to login if their password happens to end with a space. I would look into a different solution for this issue.
You can do this by adding a function to the onchange function of your html input, see https://www.w3schools.com/jsref/event_onchange.asp.
On a side note: if you also want to make things lowercase, make sure to add an exception for MxAdmin, otherwise you can't login with this account anymore.