views:

25

answers:

1

this is my group annotation attributes

    [RegularExpression(@"^[a-zA-Z0-9 _]*$", ErrorMessage = "Cannot Contains other characters ")]
    public string vcr_GroupName { get; set; }

i want to allow only two spaces in my textbox in regular expression ,how would i do that

+1  A: 

If you want to prevent 50 spaces, then just trim the content and make sure it's non blank?

Anyway, note that this: [a-zA-Z0-9 _] is written shorter as [\w ]

To use regex to only allow two spaces maximum, you can do:

^\w+(?: \w+){0,2}$

(The (?: ) part is a non-capturing group, whilst the {0,2} says repeat 2 or 1 or 0 times.)

This will also require that the first and last characters are not spaces.

(You might want something slightly different depending on your exact rules.)

Peter Boughton