views:

26

answers:

1

I need my users to input very specific cell phone format cause my app will be sending sms to them

So, I need their cell phone formats to be like this

04AB-XXXXXXX

Where A can be either 1 or 2

and B can be either 2, 4 or 6

X can be from 0-9 There must be exactly 7 numbers (X) after 04AB

It must always start with 04

Examples:

04140000000 allowed

04240000000 allowed

04340000000 not allowed

14240000000 not allowed

04170000000 not allowed

So my property would need a regular expression validator but I wouldnt even know where to start... I just got this from a tutorial but it's for a different format

[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]

this needs to be as strictly as possible because its gonna be used for text messaging. thanks in advance..

+3  A: 

you need this:

^04[12][246][0-9]{7}$

That is withouth the "-"

^04[12][246]-[0-9]{7}$

is with the "-" and

^04[12][246]-?[0-9]{7}$

allows to use the "-" or leave it

the 0-9 can be replaced with \d like (I like [0-9] for beginners readability):

^04[12][246]-?[\d]{7}$

so it can become something like:

[RegularExpression(@"^04[12][246]-?[0-9]{7}$")] 
Caspar Kleijne
Thanks Tim, for saving my day from clumsiness and bad grammar ;)
Caspar Kleijne