Search code examples
c#mudblazor

Mudblazor and C# how do I use multiple validation methods in a MudTextfield


I have the following line of code which uses a mud textbox I also have the following c# validation:

How can I call my two methods in the validation tag:

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

Solution

  • There's various ways to do this. I'd recommend using FluentValidation.

    To make your specific code work however, you just need to call the other method from the main method set on MudTextField -> Validation property.

    In the example below, I've created a new method ValidateContactNumber that calls both of your validation logic methods.

    <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))" />
    
    @code{
        private IEnumerable<string> ValidateContactNumber(string contactNumber)
        {
            if(!CheckForOnlyNumbers(contactNumber))
            {
                yield return "Contact Number must be only numbers";
            }
            foreach (var result in PhoneNumberLength(contactNumber))
            {
                yield return result;
            }
        }
        private IEnumerable<string> PhoneNumberLength(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 contactNumber)
        {
            foreach (char c in contactNumber)
            {
                if (!char.IsDigit(c))
                {
                    return false;
                }
            }
            return true;
        }
    }
    
    

    MudBlazor snippet