aspnet / jquery-validation-unobtrusive

Add-on to jQuery Validation to enable unobtrusive validation options in data-* attributes.
MIT License
257 stars 113 forks source link

Regex validation issue with select multiple #125

Closed leppie closed 2 years ago

leppie commented 4 years ago

https://github.com/aspnet/jquery-validation-unobtrusive/blob/master/src/jquery.validate.unobtrusive.js#L347

The following

return (match && (match.index === 0) && (match[0].length === value.length));

should be

return (match && (match.index === 0) && (match[0].length === match.input.length));

as value can be an array from a select with multiple attribute.

RegExp.exec will correctly convert an array into a comma-seperated string for the match and populate the input property in the result.

The following returns the same output, but the subsequent length test fails for the array version.

new RegExp("abc").exec("abc")
new RegExp("abc").exec(["abc"])

While regex is hard for this, the string case should still work exactly the same.

This should allow complex regex validation as:

// def in list
new RegExp(".*(?=def(,|$)).*").exec(["abc","def","ghi"])
// def not in list
new RegExp("(?!.*def).*").exec(["abc","def","ghi"])

Sidenote: RegularExpressionAttribute also cant deal with a list, but I can at least inherit from it to allow that.

mkArtakMSFT commented 2 years ago

Hi. Thanks for contacting us. We're closing this issue as there was not much community interest in this ask for quite a while now. You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.