Using Regular Expression in a validator_calc for an HTML field

What I am trying to do:
I have an HTML field that is looping through an array of files that the user has uploaded to the form in a separate file-upload section. For each file in the files array, the HTML field will display a text input whose value will be pre-loaded with the filename of the file. (e.g. if someone uploads a PDF file called ‘business_license’ then there will be a text input with the value of ‘business_license’ in it.) These text inputs allow the user to change the name of the file(s) if they wish prior to submitting the form. There needs to be a validation done on the filenames to ensure they are valid using Windows/Mac/Linux filename standards (no “?”, “:”, etc.)

My attempted solutions:
Regex in validator_calc
My initial thought was to use regex to accomplish this using the following pattern: ^[^\x00\/:?"<>|. ]$
I’m not great at regex, but my understanding is that this pattern should only match with strings that don’t match any of the characters listed between [ and ]. I used a validator_calc function using a lodash filter method that would run model.files all through a match() function. The problem is that FM BetterForms doesn’t like the pattern I made and claims it’s a “bad string” and won’t let the code execute at all.

This also won’t let me put an error message by the offending text input since I’d only have on errorMsg for the whole HTML field.

Regex as the validator type
I read in the docs that you could use a regex validator making use of a “pattern” key. So I tried changing my HTML field to have the following validation settings:

   "validator": "regexp",
   "pattern": "^[^\x00\\\/:*?"<>|\. ]*$"

This doesn’t work because the HTML field has multiple inputs and FM BetterForms doesn’t seem to let me specify that I want it to look at model.files in order to get the array of filenames to run against the regex pattern.

This also won’t let me put an error message by the offending text input since I’d only have on errorMsg for the whole HTML field.

@blur calling a namedAction
This is what I believe I am reduced to. In the HTML field where I am looping through model.files and generating the text inputs, I could add an @blur hook that would call my own namedAction where I can run whatever validation I choose to. This would theoretically allow me to write my own pattern without whatever BF is tripping over when it thinks my pattern is a “bad string” while also letting me add error messages directly to the offending text input. However, I’m at a loss for how to make a namedAction insert an error into the form itself. Displaying a message is easy enough, but how do I tell the form that there is an error present and not allow submission of the form until the error is cleared?

I have multiple text inputs in an HTML field that need to be validated. How can I do so most expediently and natively within FM BetterForms?

Regex should definitely work within a validator_calc function. However, since your regex contains a " character, that causes a problem when using it within JSON and likewise, using it in a validator_calc.

To resolve, you can escape the " character and proceed as usual. I put together an example that you can reference and implement into your own solution.

Let me know if you have any other questions!

Thank you! I swear I had tried taking out the " but even so, there was still the “bad string” message. But your solution worked.

[[fixed my own issue with @input not firing like it should]]

One quirk, though. My form is rendering every input with the red error styling (red border and red backgrounds on all text inputs) when yours is not. I think that can be fixed by me going into our CSS files. So I’m not as worried about this quirk.

There is a chance there is some existing CSS styling associated with errors on inputs in your environment. You can test this by checking your CSS tab and looking for anything with .error and removing it to see if that resolves your issue.