it depends by your business: if you want allow the users to create passwords with spaces at the end you should not do that.
anyway instead of trim it it would be better to validate it and notify the user about an invalid password showing the reason in this case the white spaces