1

Why is it when I specify against my ASP.NET MVC view model class the following definition:

     [StringLength(11)]
     [RegularExpression(@"‎^(09)[1-3][0-9]\d{7}$")]
     public string Mobile { get; set; }

this format is Mobile in Iran:

Start with: 09

A digit number between 1 and 3

7 digit between 0 and 9


But when I enter mobile number (for example 09124208640) ModelState isn't valid and get this error message :

The field Mobile must match the regular expression '‎^(09)[1-3][0-9]\\d{7}$'.
4
  • According to regexpal.com with the regex ^(09)[1-3][0-9]\d{7}$ and input 09124208640 it's a match. Commented Aug 1, 2013 at 6:39
  • @ta.speot.is can help me for create correct regex Commented Aug 1, 2013 at 6:44
  • I test in regular-expressions.info/javascriptexample.html and input is match. Commented Aug 1, 2013 at 6:47
  • Well your rules Start with: 09 2 ... A digit number between 1 and 3 1 ... 7 digit between 0 and 9 7 is a string that's 10 characters long. Your regex asks something different, as does the StringLength(11) constraint. Commented Aug 1, 2013 at 6:48

2 Answers 2

1

You have a typo:

"‎^(09)[1-3][0-9]\d{7]$"
                    ^

Replace ] with }.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the [0-9] part of [0-9]\d is redundant, no? Replace it with \d{8}??
@ta.speot.is, I agree with you. But you'd better comment to OP's question because the regular expression come from OP's code.
I'm not a regex guru. I was hoping someone that was could incorporate my feedback :)
0

From this answer, consider the following regex:

^09[1|3][0-9]{8}$

[1|3] matches 1, | or 3. This seems erroneous, leading us to:

^09[13][0-9]{8}$

From your comment, if you need to accept 2 in the third digit:

^09[1-3][0-9]{8}$

10 Comments

tnx for help me,but this regex is't match and i have same error :(
[13] does not match 2. BTW, OP did not used [1|3].
@falsetru I know, I'm working off what Wikipedia says. Iranian mobile phone numbers don't have 2 as their third character.
@ta.speot.is, OP wants A digit number between 1 and 3.
Original Poster. This guy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.