Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,
i want to apply validation for textbox, it should accept starting number 1300 or 1800 only.. this is my requirement
please provide soultion regarding this

What I have tried:

[Required(ErrorMessage = "Please enter the Telephone")]
       [RegularExpression("(^1300\d{6}$)|(^1800\d{6}$)", ErrorMessage = "Entered number must be start with 1300 or 1800")]
       [Display(Name = "Telephone *")]
       public string Telephone { get; set; }
Posted
Updated 18-Aug-17 3:36am
Comments
Patrice T 18-Aug-17 4:35am    
What is the problem with this RegEx ?
VenkataSeshu B 18-Aug-17 5:39am    
it is not taking \ symbol before d{6}
Patrice T 18-Aug-17 5:42am    
Show a sample of what you want to match.

Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

C# has two types of string literals - regular and verbatim.
String Literals | C# Language Specification | Microsoft Docs[^]

You are using a regular string literal, which means you need to escape certain characters by prefixing them with \. For a literal \, you need to double it up:
C#
[RegularExpression("(^1300\\d{6}$)|(^1800\\d{6}$)", ErrorMessage = "Entered number must be start with 1300 or 1800")]

Alternatively, you could use a verbatim string literal by prefixing the string with @:
C#
[RegularExpression(@"(^1300\d{6}$)|(^1800\d{6}$)", ErrorMessage = "Entered number must be start with 1300 or 1800")]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900