You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
I'm unable to change the validation rules for the password field in the users collection. #11982
I want to use the default authentication method in the payload, but I also need to set custom validations on the password field. However, I don't see any way to do this since I don't have direct access to the password field. By default, the only validations applied are required and a minimum length of 3 characters.
At the very least, there should be an option to adjust the minimum character requirement (e.g., increasing it from 3 to 8). Additionally, the ability to define custom validation rules for passwords would be highly beneficial.
Hi @MDrzal1886 - you can set up custom validation for the password field using the beforeValidate hook on your users collection. Here's an example of how to add it and apply the validation:
import { ValidationError } from 'payload' // use this to throw the error on the password field
export const Users: CollectionConfig = {
slug: 'users',
auth: true,
hooks: {
beforeValidate: [
({ data: { password } = {} }) => {
if (!password) {
return
}
let message: string | undefined
if (password.length < 8) {
message = 'Password must be at least 8 characters long.'
}
// you can add more conditions and update the error message as needed
const hasSymbols = /[$-/:-?{-~!"^_`[\]]/.test(password)
if (!hasSymbols) {
message = 'Password must include at least one symbol.'
}
if (message) {
throw new ValidationError({
collection: 'users',
errors: [{ message, path: 'password' }],
})
}
},
],
},
// ... rest of config
}
The output of this validation would look like this:
@JessChowdhury
Thanks a lot, but I still have two issues with this solution:
When I enter only two characters, I see a “minimum 3 characters” validation message, which I believe is incorrect behavior, since the validation should require a minimum of 8 characters.
The validation only runs when the button is clicked, not while typing.
Activity
JessRynkar commentedon Apr 8, 2025
Hi @MDrzal1886 - you can set up custom validation for the password field using the
beforeValidatehook on your users collection. Here's an example of how to add it and apply the validation:The output of this validation would look like this:

Hope this helps!
MDrzal1886 commentedon Apr 9, 2025
@JessChowdhury
Thanks a lot, but I still have two issues with this solution:
github-actions commentedon Apr 17, 2025
This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.