C # regex help - checking input

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

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

Can I do this with Regex.Match?

+5
source share
1 answer

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);
}
+7
source

All Articles