Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# regex help - validating input

Tags:

c#

regex

I need to validate that the user entered text in the format:

####-#####-####-###

Can I do this using Regex.Match?

like image 883
Steven Avatar asked Nov 03 '10 15:11

Steven


Video Answer


1 Answers

I would do something like this:

private static readonly Regex _validator = 
    new Regex(@"^\d{4}-\d{5}-\d{4}-\d{3}$", RegexOptions.Compiled);
private static bool ValidateInput(string input)
{
    input = (input ?? string.Empty);
    if (input.Length != 19)
    {
        return false;
    }
    return _validator.IsMatch(input);
}
like image 63
ChaosPandion Avatar answered Oct 08 '22 02:10

ChaosPandion