开发者

C# regex help - validating input

开发者 https://www.devze.com 2023-01-23 13:18 出处:网络
I need to validate that the user en开发者_Python百科tered text in the format: ####-#####-####-###

I need to validate that the user en开发者_Python百科tered text in the format:

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

Can I do this using Regex.Match?


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消