Alpaca JS doesn't take into consideration if the regex pattern contains length limit

347 views Asked by At

I am building a web application, using alpaca.js. I notice a problem when using regex patterns. If the pattern contains length it is not taken into consideration. For example this pattern "[0-9]{5}" should permit only 5 numbers, but it validates the numbers and not the length. To fix this I added min and max length, but it is a big problem if i have this kind of pattern ^[0-9]{1,3}([a-zA-Z]?)$. Did somebody faced this issue before? Thanks

1

There are 1 answers

1
Oussama BEN MAHMOUD On BEST ANSWER

What you do is correct but, this will be used for schema validation and in your example you maybe need a mask rather than a regex. The mask will allows a user to more easily enter fixed width input where you would like them to enter the data in a certain format (dates,phone numbers, etc)

"options": {
  "fields": {
    "cap": {
      "maskString": "99999" // 9 is a number, and here the mask will supports 5 digits
    }
  }
}

To use the maskString option you need first to import/add the Masked Input Plugin (the website is down, you could grab it from CDNJS or github)

For more details on maskString see Example 4 in Alpaca text field documentation. For more details on Masked Input Plugin see this github repository.

And here's a fiddle for your example. Hope this helps you.