Link to home
Start Free TrialLog in
Avatar of Sailing_12
Sailing_12

asked on

Regex evaluation failing using Data Annotations in MVC app.

I am trying to use Data Annotations and regex to validate an address field in a .net MVC application (to disallow the word 'suite' in any form).

I have the following definition in my annotations class:
 
    [MetadataType(typeof(LocationMetaData))]
    public partial class Location
    {

    }

    public class LocationMetaData
    {
        [RegularExpression(@"^(?:(?!\bsuite\b).)*$/ig", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]
        public String street1 { get; set; }
    }

Open in new window


...which should only match when the string does not contain 'suite'.

I am getting the failed error message no matter what I enter in the field.

I have tried these variations on the Regex:
^((?!suite).)*$/ig
/^((?!suite).)*$/ig
/^((?!\b(\w*suite\w*)\b).)*$/ig

These all work in online regex testers, but fail in my app.

Does anyone have a working solution for this?

Thanks.
Avatar of Sailing_12
Sailing_12

ASKER

I should point out that I have other regex validations in the LocationMetaData class that are working fine (phone, FAX, Zip) so all of the framework seems properly configured.
Avatar of Terry Woods
Try:
 ^(?!.*suite)/ig

Open in new window

That didn't work either.

User generated image
Apologies, I just noticed I only had one delimiter (the / character).

Try:
/^(?!.*suite)/ig

Open in new window

or, if delimiters aren't needed, then this may work:
^(?!.*suite)

Open in new window

but you would need to ensure that case insensitivity was activated.
Sometimes when pattern delimiters aren't needed, you can still activate the case insensitive mode like this:
(?i)^(?!.*suite)

Open in new window

These fail as well.
I don't think it's the pattern that is the problem here, I think it's how the expression is being processed as an annotation.
Have you got some examples of how other fields are validated with regex?
Here is the full LocationMetaData class
    public class LocationMetaData
    {
        [RegularExpression(@"^([0-9]{3})-([0-9]{3})-([0-9]{4})$", ErrorMessage = "Invalid FAX number format")]
        public String locationFax { get; set; }

        [RegularExpression(@"^([0-9]{3})-([0-9]{3})-([0-9]{4})$", ErrorMessage = "Invalid Phone number format")]
        public String locationPhone { get; set; }

        [RegularExpression(@"^([0-9]{5})$", ErrorMessage = "Invalid Zip code format")]
        public String zip { get; set; }

        [RegularExpression(@"(?i)^(?!.*suite)", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]
        public String street1 { get; set; }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
.NET regex doesn't use delimiters. If you want to include options, then you need to use the appropriate syntax:

[RegularExpression(@"(?ig)^(?:(?!\bsuite\b).)*$", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]

https://www.regular-expressions.info/modifiers.html
Terry - that one works.

kaufmed - yours errors out when I run it - {"parsing \"(?ig)^(?:(?!\\bsuite\\b).)*$\" - Unrecognized grouping construct."}
Sorry, I forgot that "g" doesn't mean anything in .NET regex either. Regex searches are global if you use the correct method of the regex class; since the data annotation only uses the Match method, you wouldn't be doing a global search anyway.

[RegularExpression(@"(?i)^(?:(?!\bsuite\b).)*$", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]

Open in new window


In re-reading your question, that pattern won't do what you expect anyway. To match a string that doesn't contain "suite":

[RegularExpression(@"(?i)^(?!.*suite).*$", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]

Open in new window

Glad to hear you got it solved... don't forget to accept the solution :-)