0

Im using MVC with Data Annotations enabled. In my ViewModel I have a property that contains a regular expression validation rule to only allow numbers

[RegularExpression("@([0-9])", ErrorMessage = "Invalid. Must use only numbers.")]

When I submit my form the ModelState.IsValid is returning as false. When I added a breakpoint on my ModelState in the controller I saw that the StreetNumber property of the ViewModel that I decorated with the regular expression data annotation was failing (even though I entered the number 84 in the form field).

Does anyone have any ideas as to why this would still be failing. (I have verified that this property was the only one that was errounously failing).

Sparky
  • 98,165
  • 25
  • 199
  • 285
Dean Forant
  • 61
  • 1
  • 9

2 Answers2

1

You need anchors and multiple digits:

[RegularExpression("@(^[0-9]+$)", ErrorMessage = "Invalid. Must use only numbers.")]
//           here  ___^     ^^
Toto
  • 89,455
  • 62
  • 89
  • 125
  • I changed the regular expression to include "@(^[0-9]+$)" but it still seems to be failing on that property. With the message "Invalid. Must use only numbers," – Dean Forant Mar 20 '15 at 15:00
  • @DeanForant: Sorry, I don't know how the validation works. may be you must reverse the test `[^0-9]`? Is the error message coming when the string is matched or when it is not matched? – Toto Mar 20 '15 at 15:47
  • Its not a matter of matching the string. The regex. is supposed to allow only numbers but even when I enter a number in the from field that is bound to the property in the model that has this regex rule it fails. But after looking at the property its for an street number of an address. I can make a strong case to my project manager that not all street numbers are only numbers. for example 12 Elm st. or 12B Elm street. In fact I used to live at an address that had a letter after the number (22B). If I can make the case for this to my Project manager than we can eliminate this validation check. – Dean Forant Mar 23 '15 at 14:22
  • I tried making the case for removing the validation rule altogether but the street number must be a Number (due to some algorithm that is being run to determine a particular district (Neighborhood). Having said that I put the following regex (^[0-9]+$) in regexpal.com and the test for numbers passed whereas letters did not. I'm stilff quite baffled why its failing in MVC on the data annotation for the propertie of the model. – Dean Forant Mar 24 '15 at 14:40
  • Here is my property that im having problems with in my Model class for anyone who would like to review it: [DisplayName("Street #")] [RegularExpression("@([0-9]*)", ErrorMessage = "Invalid. Must use only numbers.")] public string StreetNumber { get; set; } – Dean Forant Mar 24 '15 at 14:40
1

I figured it out. It turned out to be a very obscure oversight. The DataAnnotation for the Regular expression should have the @ before the quotation marks.

Instead of this: [RegularExpression("@(^[0-9]+$)", ErrorMessage = "Invalid. Must use only numbers.")]

I need to have this: [RegularExpression(@"(^[0-9]+$)", ErrorMessage = "Invalid. Must use only numbers.")]

I really need to get a bigger coffee mug :)

BenV
  • 12,052
  • 13
  • 64
  • 92
Dean Forant
  • 61
  • 1
  • 9