smnbss
smnbss

Reputation: 1041

jquery unobrtusive validation & regex modidifers

is there a way to pass the RegExp modifier to jquery unobtusive validation? I want to make a regex case insensitive.

RegExp's constructor contains a modifier parameter that can be set to i|g|m:

Modifier    Description
i           Perform case-insensitive matching
g           Perform a global match (find all matches rather than stopping after the first match)
m           Perform multiline matching

so it would be nice to be able to do

<input type="text" name="Property_FullPostCode" 
            data-val="true" data-val-required="Invalid postcode" data-val-regex="Invalid postcode"
               data-val-regex-pattern="myregex" **data-val-regex-modifiers="i"** />

is there a way to do so without having to modify the jquery.validate.unobtrusive.js or add a new validation mode?

Upvotes: 0

Views: 1278

Answers (1)

smnbss
smnbss

Reputation: 1041

My quick fix was to change jquery.validate.unobtrusive.js to:

$jQval.addMethod("regex", function (value, element, params) {
        var match;
        if (this.optional(element)) {
            return true;
        }
        **var modifiers = $(element).data('val-regex-modifiers');**
        match = new RegExp(params**, modifiers**).exec(value);
        return (match && (match.index === 0) && (match[0].length === value.length));
    });

Upvotes: 1

Related Questions