Class-validator: question: How do you check a string if it contains at least 1 number, at least 1 of these special character !@#$%^&, and at least 1 Upper Case

Created on 18 Apr 2020  路  3Comments  路  Source: typestack/class-validator

Hello. thank you for this awesomeness. just a question. How would you check a string with the said conditions? Contains() cant accept an array of strings. Should I create a custom validator for this?

Use case:
Check a password before saving it in the database.

Thank you sir!

question

Most helpful comment

Hi,

you can add several @Contains, but because your task sounds more like regexp I would suggest to use @Matches.

@Matches(/[0-9]/)
@Matches(/[!@#$%^&]/)
@Matches(/[A-Z]/)
public password: string;

All 3 comments

Hi,

you can add several @Contains, but because your task sounds more like regexp I would suggest to use @Matches.

@Matches(/[0-9]/)
@Matches(/[!@#$%^&]/)
@Matches(/[A-Z]/)
public password: string;

@satanTime thank you man. I didnt that on the documentation last night. Maybe was just tired. thanks again. will close this now.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

st-clair-clarke picture st-clair-clarke  路  3Comments

otbe picture otbe  路  5Comments

golozubov picture golozubov  路  5Comments

yantrab picture yantrab  路  3Comments

renehauck picture renehauck  路  5Comments