In my model class, I have a property called ContactNo
. I want a regular expression for validation purposes. It should validate that the property starts with "01" and has 11 characters, all numerical.
Asked
Active
Viewed 752 times
-4

Steve Konves
- 2,648
- 3
- 25
- 44

Waheed
- 11
- 2
-
1You do not need a regex for that. Check numeric with `.All(p => Char.IsDigit(p))`, `01` with `.StartsWith("01")` and length with `.Length == 11`. You can even set different error messages with this approach. – Wiktor Stribiżew Aug 12 '16 at 14:26
-
You should checkout this [question](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) and its answers, if true it is on the tangent of your question, it should be helpful to you – Luiso Aug 12 '16 at 14:37
2 Answers
3
The RegEx for the above
^01[0-9]{9}$
[0-9]{9}
because after 01 the remaining length should be 9 numbers
You can check the site http://regexr.com/ helps out a lot with RegEx especially if you are a beginner like me.

ZerosAndOnes
- 1,083
- 1
- 13
- 25
-
1I doubt it will be working for OP. See [your regex demo](http://regexstorm.net/tester?p=01%5b0-9%5d%7b9%7d&i=015555555550155555555501555555555015555555550155555555501555555555). It does not check the length at all. – Wiktor Stribiżew Aug 12 '16 at 14:39
-
@WiktorStribiżew Thank you for pointing that out, added the anchors to check the exact length. – ZerosAndOnes Aug 12 '16 at 14:54
-
Those are incorrect anchors for validation. Now, you allow a newline symbol at the end of the string. See http://ideone.com/jsgVlE – Wiktor Stribiżew Aug 12 '16 at 14:55
-
@WiktorStribiżew I tried to overcome the RegEx from accepting a newline symbol at the end but to no avail. Is there a way to possible do that only using RegEx? – ZerosAndOnes Aug 12 '16 at 16:03
-
[Look here](https://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx), you will quickly find it. – Wiktor Stribiżew Aug 12 '16 at 16:10
-
@WiktorStribiżew Is it @"\A01[0-9]{9}\z"?Thanks a bunch for increasing my knowledge in any case :D – ZerosAndOnes Aug 12 '16 at 16:17
1
if you need regex, try this:
^01[0-9]{9}\z

ROman Ryabko
- 41
- 2
- 4
-
1Роман, are you sure? See [this demo](http://regexstorm.net/tester?p=%5e01%5cd%7b9%7d%24&i=01%d9%a1%d9%a2%d9%a3%d9%a4%d9%a5%d9%a6%d9%a7%d9%a8%d9%a9). Also, your regex also allows a newline at the end of the string. – Wiktor Stribiżew Aug 12 '16 at 14:38
-
1
-
And here is another "nice": http://ideone.com/jsgVlE. As I said, the regex allows a newline at the end of the input. – Wiktor Stribiżew Aug 12 '16 at 14:57
-
-
There is no need for the `RegexOptions.Singleline` flag, there is no dot anywhere in the pattern. You wanted to say "add `RegexOptions.ECMAScript` flag" - that is a good idea as it will [fix the issue with `\d`](http://regexstorm.net/tester?p=%5e01%5cd%7b9%7d%24&i=01%d9%a1%d9%a2%d9%a3%d9%a4%d9%a5%d9%a6%d9%a7%d9%a8%d9%a9&o=e). Waheed, are you sure you want to allow a string with a newline at the end? – Wiktor Stribiżew Aug 12 '16 at 15:12
-