el shorty
el shorty

Reputation: 237

Yup schema validation with regex keeps giving me field errors

So I have a problem with my yup validation. I want to make sure that for my name field the user can enter only letters or spaces. However no matter what I type in my field it keeps telling me it is incorrect.

const letterRegex = new RegExp("/^[a-zA-Z\s]*$/");

const pg_BinnenCoLoondienst: yup.SchemaOf<RowBinnenCoLoonDienstData> = yup
  .object()
  .shape({
    REV_PE_BCL_Naam: yup.string().required("Name is required").matches(letterRegex, "Name can only contain letters & spacces"),

If I remove the matches part and only use required than it works perfectly so the issue is with the 'matches' part of the validation.

result

Anyone sees what I'm doing wrong? Thx!

Upvotes: 0

Views: 735

Answers (1)

Neetigya Chahar
Neetigya Chahar

Reputation: 693

You can use this regex

const letterRegex = new RegExp("^[a-zA-Z ]*$");

Upvotes: 1

Related Questions