verygoodsecurity / vgs-collect-js

VGS Collect.js script loading module
https://www.verygoodsecurity.com/
7 stars 14 forks source link

How to use regex validation? #67

Open ddinchev opened 11 months ago

ddinchev commented 11 months ago

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 commented 9 months ago

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}$/"],
  ...
});