NewOldMax / react-form-validator-core

Core validator component for react forms
MIT License
95 stars 44 forks source link

How to pass an actual regex? #24

Closed cyberwombat closed 6 years ago

cyberwombat commented 6 years ago
matchRegexp: (value, regexp) => {
        const validationRegexp = (regexp instanceof RegExp ? regexp : (new RegExp(regexp)));
        return (!isExisty(value) || isEmpty(value) || validationRegexp.test(value));
    },

I don't see how one can pass a regex? For example I am wanting to use modifiers such as the case insensitive 'i' but ['matchRegep:/^[a-z0-9]+$/i'] gets handled as a string and doesn't work. I saw an issue on formsy react with a similar unanswered question so perhaps it stems from that.

I am able to do what I need using [A-Za-z0-9] for my needs so this is more of a general curiosity question.

NewOldMax commented 6 years ago

It's not possible atm, but you can create your own validation rule and do whatever you want

ValidatorForm.addValidationRule('customRegexp', (value) => {
    const regex = new RegExp('ab+c', 'i');
    return regex.test(value);
});