1
 [Required(ErrorMessage = "Please Enter AccountZip Code!")]
            [RegularExpression(@"/(^\d{5}(-\d{4})?$/", ErrorMessage = " Zip code must be 5 characters length")] 
            public string AccountZip { get; set; }

I did regularexpression for Zip code validation I am getting this Error Message

parsing "/(^\d{5}(-\d{4})?$/" - Not enough )'s.

Can any body help me out?

Thanks

tereško
  • 56,151
  • 24
  • 92
  • 147
user300485
  • 525
  • 5
  • 10
  • 24

3 Answers3

9

Your regex looks like it was pulled from a javascript sample. Try this:

@"^\d{5}(-\d{4})?$"
Keith
  • 5,100
  • 3
  • 31
  • 48
4

You need one more ( at the end as follows:

RegularExpression(@"/(^\d{5}(-\d{4})?)$/"
Leons
  • 2,524
  • 1
  • 18
  • 24
  • how to restrict to enter only Numbers? not characters in to that field? – user300485 May 17 '11 at 15:17
  • The RegEx will only match if the field contains numbers, but if you want to prevent the user from entering anything other than numbers, then you need JavaScript of JQuery code on the client side to stop them. Here is a link to a plugin that might help. http://igorgladkov.com/jquery/validator.html – Leons May 17 '11 at 15:23
  • Leons I dont think we need Client Side code to stop them. here is the thing i updated my code with this code.. @"\b(0?[0-9][0-9][0-9][0-9][0-9])\b" this will allow only numbers..thanks for your time – user300485 May 17 '11 at 15:24
  • @user300485: wouldn't that allow "this text too 00000"? Might need to anchor it with `^$` instead of `\b` (unless these attributes do that for you) – Qtax May 17 '11 at 16:32
1

Great job @"\b(0?[0-9][0-9][0-9][0-9][0-9])\b" actually works and validates both numeric and length

JosephDoggie
  • 1,320
  • 3
  • 19
  • 44