Does radzen number only validator exist? or phone number?

Hi, im trying to use radzens validator to validate a radzen grids fields.

I've got a field with UK phone numbers inside it and was wondering how to validate users input if they put for example letters inside? I want to accept numbers only and a length of 11 digits only.

Here is what i've got so far:

<RadzenGridColumn TItem="People" Property="Number" Title="Number">
                    <EditTemplate Context="engineer">
                        <RadzenTextBox @bind-Value="engineer.Number" Style="width:100%; display: block" Name="Number" />
                        <RadzenRequiredValidator Text="Engineers number is required" Component="Number" Popup="true"/>
                        <RadzenLengthValidator Text="Numbers should contain 11 digits" Component="Number" Popup="true" Min="11" Max="11"/>
                    </EditTemplate>
                </RadzenGridColumn>

But if a user enters 11 letters and so, it will still accept this.

Also, I was wondering if we can make our own custom validator if the above is not possible?

Thank you

Hi @420BlazorIt,

You can probably try the RegExValidator. You can also create a validator component of your own. Check some of the existing validators for ideas.

1 Like

Hi Korchev,
Are there any existing validators which can check if user input is unique? for example if user tries to enter email address which already exist in the radzen grid table, it will show up an error message which says email is not unique or something like this?
Thank you

No, we don't have such a validator.

is this possible to do by creating a custom validator? if so, could you point me in the right direction for resources on how to create the custom validator please? thanks

Check the implementations that I linked above. You can create a custom validator and set all existing emails as a property and then check against if that property contains the component value. Something like:

namespace MyApp
{
    public class UniqueEmailValidator : Radzen.Blazor.ValidatorBase
    {
        [Parameter]
        public override string Text { get; set; } = "Email exists";

        protected override bool Validate(IRadzenFormComponent component)
        {
            var email = component.GetValue();

            return email != null && Emails != null && !Emails.Contains(email.ToString());
        }

        [Parameter]
        public IEnumerable<string> Emails { get; set; }
    }
}
<RadzenTextBox @bind-Value=@email Name="EmailTextBox" />
<UniqueEmailValidator Component="EmailTextBox" Emails=@emails />

@code {
     IEnumerable<string> emails = GetExistingEmails();
}
2 Likes

that looks great! will give it a go thank you for your help

hi korchev! I have successfully implemented this and it is working great however, I have 1 issue, when I edit the data and hit save changes, it says the number already exist, how can I overcome this issue?

I think this is application specific logic which you have to implement :slight_smile: Find a way to pair emails with people when checking for uniqueness.

alright then! thank you for your help!

In this example, you can simply remove the && !Emails.Contains(email.ToString()) part. It compare your entered value with IEnumerable<string> Emails.