In Regula, how can I have a constraint that does NOT match a pattern? I can use @Pattern like this:
<input type="text" id="categoryId" data-constraints="@Pattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/)" />
But let's say that /[0-9]-[A-Z]{3}-[0-9]{4}/ is a "bad" pattern and I want to allow them to enter anything that doesn’t match that pattern.
In regular JavaScript I can do:
if(!/[0-9]-[A-Z]{3}-[0-9]{4}/.test(value)) {
...
}
How can I do it in Regula?
There are a few ways to do this. For your case, you can use a negative lookahead:
I'm not sure how that works for more-complex regular expressions, but if that's the case, I guess you could create a custom constraint:
You can even defer to the inbuilt
@Patternvalidator in your validator function, like so:Then you can use it in your input element like so:
I suggest the second approach because you can pass in parameters that the in-built
@Patternvalidator supports, likeflagsfor regular-expression flags. This is also a proper inverse of the inbuilt validator.EDIT: I think it might be useful to add an optional parameter to
@Patternso that you can invert the pattern. So basically (assuming this feature was implemented) all you would have to do is this:I'll put this on my list of things to do.