You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MudBlazor与C#:如何在MudTextField中调用多个验证方法

如何在MudTextField的Validation属性中同时调用两个验证方法

我现有一段使用MudTextField的代码,同时编写了两个C#验证方法PhonNumberLength和CheckForOnlyNumbers,想知道怎么在MudTextField的Validation属性标签中同时调用这两个方法。

现有代码:

<MudTextField @bind-Value="@golfReg.ContactNumber" Required="true" Label="Contact Number" Variant="Variant.Filled" Margin="Margin.Dense" Style="@($"color:#00679F;background:{Colors.Shades.White};")" Validation="@(new Func<string, IEnumerable<string>>(PhonNumberLength))" />
private IEnumerable<string> PhonNumberLength(string ContactNumber) {
        if(string.IsNullOrWhiteSpace(ContactNumber)) {
            yield return "Contact Number is required!";
            yield break;
        }
        if(ContactNumber.Length > 13) {
            yield return "Contact Number must be a max length of 13 digits";
        } else if(ContactNumber.Length < 10) {
            yield return "Contact Number must be a min length of 10 digits";
        } else if(!ContactNumber.All(c => char.IsDigit(c))) {
            yield return "Contact Number must only contain numeric values";
        }
    }

public bool CheckForOnlyNumbers(string input)
{
    foreach (char c in input)
    {
        if (!char.IsDigit(c))
        {
            return false;
        }
    }

    return true;
}

解决方案

MudTextField的Validation属性要求传入一个返回IEnumerable<string>的委托(用于输出错误提示文本),而CheckForOnlyNumbers返回的是bool,所以需要把两个验证逻辑整合到一个符合要求的方法中,以下是两种可行方式:

方式1:创建合并验证方法

新建一个方法,同时调用两个原有验证逻辑,并收集所有错误消息:

private IEnumerable<string> ValidateContactNumber(string contactNumber)
{
    // 先执行长度验证,收集错误
    foreach (var error in PhonNumberLength(contactNumber))
    {
        yield return error;
    }

    // 非空时执行数字格式验证,不通过则添加错误
    if (!string.IsNullOrWhiteSpace(contactNumber) && !CheckForOnlyNumbers(contactNumber))
    {
        yield return "Contact Number can only contain numeric characters";
    }
}

然后修改MudTextField的Validation属性指向这个新方法:

<MudTextField @bind-Value="@golfReg.ContactNumber" Required="true" Label="Contact Number" Variant="Variant.Filled" Margin="Margin.Dense" Style="@($"color:#00679F;background:{Colors.Shades.White};")" Validation="@(new Func<string, IEnumerable<string>>(ValidateContactNumber))" />

方式2:直接整合到现有方法

可以直接在PhonNumberLength中调用CheckForOnlyNumbers的逻辑,无需新建方法:

private IEnumerable<string> PhonNumberLength(string ContactNumber) {
    if(string.IsNullOrWhiteSpace(ContactNumber)) {
        yield return "Contact Number is required!";
        yield break;
    }
    if(ContactNumber.Length > 13) {
        yield return "Contact Number must be a max length of 13 digits";
    } else if(ContactNumber.Length < 10) {
        yield return "Contact Number must be a min length of 10 digits";
    }
    // 调用数字格式验证,不通过则添加错误
    if(!CheckForOnlyNumbers(ContactNumber)) {
        yield return "Contact Number must only contain numeric values";
    }
}

这种方式下,原有MudTextField的代码无需修改,就能同时执行两种验证。

注意事项

  • 空值判断要前置,避免CheckForOnlyNumbers处理空字符串(必填验证已经覆盖空值场景)
  • MudBlazor会自动收集所有返回的错误消息,并显示在输入框下方

内容的提问来源于stack exchange,提问作者Zain Nabi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 10:42:23