Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use regex validation? #67

Open
ddinchev opened this issue Oct 12, 2023 · 1 comment
Open

How to use regex validation? #67

ddinchev opened this issue Oct 12, 2023 · 1 comment

Comments

@ddinchev
Copy link

ddinchev commented Oct 12, 2023

Hey there,

I'm trying to validate US zip code which is 5 digits. In the collectjs docs there isn't a min/max length field validator, I can easily write a regex that expect 5 digits exactly and the docs say there is regExp field validation but no example is given on how it works. Even your own form builder produces an error when "regExp" validator is selected for a new field:
https://forms.vgs.dev/

Uncaught (in promise) Error: Invalid validations: regExp

Can you provide a working example?

@ryanmark
Copy link

ryanmark commented Dec 1, 2023

I figured out how this works through trial and error. You pass a regex literal in a string in the validations array:

form.field("#zip", {
  ...
  validations: ["required", "/^[0-9]{5}$/"],
  ...
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants