Click here to Skip to main content
15,896,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement in my project where the input will be 1;827364726 or 2;34833560.
i.e the input must starts with either with 1 or 2 and a delimitor(;)and then any number from 0-9.I want to validate the input before processing.So i want to use regular expression pattern something like as shown below.The below regular expression pattern is not working

var patt=/[12];[0-9]/

Is there anyway to validate my input in C# or i must go with regular expression pattern?

Thanks
Prashanth
Posted

you can use this patt
C#
^[12];\d+$
 
Share this answer
 
v2
Comments
cherlaprashanth 8-Jan-12 10:12am    
The regular expression is working fine.Thank U.
Can we do the same with any of the string methods?
cherlaprashanth 8-Jan-12 11:11am    
Thanks for the answer
Neema Derakhshan 9-Jan-12 6:36am    
your wellcome but actually I did'nt try string methods :)
With regular expressions you could possibly use something like:
[12];(\d){8,9}

Another option would be to split the text based on the semicolon and check that there are two return elements, the first one has number 1 or 2 and the second one is integer. Methods to use with this would be:
- String.Split[^]
- Int32.TryParse[^]
 
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