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.
You can then use the trim() function to trim spaces, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim.
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.