I want to check the input of a textbox for the following patterns:
***** or [0-9] anything else should return false using regular expressions. How can I do this in C#.
How would I make the check?
Thanks,
Ben
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Javeed M ShaikhPosted Nov 14, 2011, 3:54 PM
using System.Text.RegularExpressions;
string input;
string pattern = @"^([\*]){5}$|[0-9]";
input = Console.ReadLine();
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("all ok");
}
else
{
Console.WriteLine("error");
}
Console.ReadKey();
Ben JonesPosted Nov 14, 2011, 4:22 PM
This is perfect.
VulpesPosted Nov 14, 2011, 4:01 PM
If you do, then it will simply be:
string pattern = @"^\d{5}$";
bool isValid = Regex.IsMatch(textBox1.Text.Trim(), pattern);