-2

I want to validate an 11 digit cellphone number which must have 0 and 3 first and then 9 more digit after 03 for example 03XXXXXXXXX

    [Column("ContactNo")]
    [Required(ErrorMessage = "Contact No is required..."), Display(Name = "Contact No")]
    [RegularExpression(@"^\[0]{1}\[3]{1}\d{9}$", ErrorMessage = "Invalid Contact No")]
    public decimal ContactNo { get; set; }
Farman Ud Din
  • 189
  • 4
  • 20
  • 1
    `@"^03\d{9}$"`. Do not escape the first two `[`, or you corrupt the character classes. – Wiktor Stribiżew Jan 12 '18 at 12:21
  • @WiktorStribiżew: Why did you choose that duplicate? It is nothing to do with this question – musefan Jan 12 '18 at 12:23
  • @musefan all three explain all OP needs to know how to not make such typos. – Wiktor Stribiżew Jan 12 '18 at 12:23
  • I have try this but it also not working – Farman Ud Din Jan 12 '18 at 12:24
  • @WiktorStribiżew: So does a book on regular expressions, but that isn't how SO works. OP has a specific problem and has provided specific code. So unless there is a question about validating numbers with his same format, then I don't see why you have closed this as a dupe – musefan Jan 12 '18 at 12:25
  • 2
    @musefan: The OPs problem is actually that they don't understand the regex they have in front of them. If they understood what that regex did then they would know why it wasn't working and be able to fix it. I'd have been nervous about closing as duplicate myself but I can certainly see why it has been done. – Chris Jan 12 '18 at 12:27
  • 3
    @Chris: You are joking right?? You could close every single question on stack overflow with the logic of "if the OP understood what they were doing they wouldn't need to ask". None of those dupes help the OP to understand what they have done wrong, and how to fix the problem. Obviously we can say "go learn stuff" to every question, but then we wouldn't have a stack overflow would we – musefan Jan 12 '18 at 12:29
  • @musefan ... very correct ... all we are here for helping each other and not to closing questions – Sarabjit Singh Jan 12 '18 at 12:36
  • @musefan: I am not joking, I genuinely think that was the logic Wiktor was working by. As I said I wouldn't have done it myself and I may well be misrepresenting Witor. I'll let him argue the pros and cons of his actions if he wants to. – Chris Jan 12 '18 at 12:42
  • @SarabjitSingh: People closing questions as duplicates are genuinely doing it because they think the duplicate will answer the question for the OP. I'd agree that closing for other reasons often isn't helpful to the user but duplicate is specifically somebody saying "I believe your answer can be found here" - suggesting that Wiktor (and anybody else closing as duplicate) isn't trying to be helpful seems slightly uncharitable. – Chris Jan 12 '18 at 12:44
  • Thank all of you guys I solve my problem. The problem is not with the regex sorry actually the problem is datatype. Thanks for the suggestions I appreciate it. But don't close question as duplicate cause every has some problem in the code which may be syntax problem or may be logical problem. thank u and think about it – Farman Ud Din Jan 12 '18 at 13:55

1 Answers1

2

The problem with your regex is that you are using the escape character when you don't need it (This is the backslash). The only one you need is the \d which is used to denote a decimal character.

In this specific case, \[ means that you want to escape the normal usage of [ and treat it like a literal character.

So you need to remove the backslashes that are used before the square brackets. So \[ should become [.

However, you don't need to even use square brackets when you want a single literal character, you can just use 0 and 3 on their own for example.

With all that in mind, the following regex should work for your requirements:

^03\d{9}$

You can see this in action here.


I would suggest you take some time to read about escape characters and when and where you should use them. This may be a good place to start, though there are certainly plenty of good resources out there.

musefan
  • 47,875
  • 21
  • 135
  • 185