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:
...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.
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; }
}
...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).)
These all work in online regex testers, but fail in my app.
Does anyone have a working solution for this?
Thanks.
Try:
^(?!.*suite)/ig
Apologies, I just noticed I only had one delimiter (the / character).
Try:
Try:
/^(?!.*suite)/ig
or, if delimiters aren't needed, then this may work:^(?!.*suite)
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)
ASKER
These fail as well.
ASKER
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?
ASKER
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; }
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.NET regex doesn't use delimiters. If you want to include options, then you need to use the appropriate syntax:
https://www.regular-expressions.info/modifiers.html
[RegularExpression(@"(?ig)^(?:(?!\bsuite\b).)*$", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]
https://www.regular-expressions.info/modifiers.html
ASKER
Terry - that one works.
kaufmed - yours errors out when I run it - {"parsing \"(?ig)^(?:(?!\\bsuite\\b) .)*$\" - Unrecognized grouping construct."}
kaufmed - yours errors out when I run it - {"parsing \"(?ig)^(?:(?!\\bsuite\\b)
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.
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)^(?:(?!\bsuite\b).)*$", ErrorMessage = "Invalid Street1 format - Enter suite information in Street2")]
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")]
Glad to hear you got it solved... don't forget to accept the solution :-)
ASKER