How can I limit the text input only to allow English characters and numbers?

Platform: Blazor| Category: Components

You can use data-annotation to apply the regular expression for limiting the characters and numbers in blazor app.

In the following code, allow the char and numbers in textbox component. The RegularExpression attribute validates that the property value matches a specified regular expression.

<EditForm Model="@model" OnValidSubmit="handleSubmit">
    <DataAnnotationsValidator></DataAnnotationsValidator>
    <InputText @bind-Value="@model.Name"></InputText>
    <ValidationMessage For="()=>model.Name"></ValidationMessage>
    <button type="submit">submit</button>
</EditForm>

@code {

    private Countries model = new Countries();

    public class Countries
    {
        [Required]
        [RegularExpression(@"^[a-zA-Z0-9 ]+$", ErrorMessage = "Special characters are not accepted.")]
        public string Name { get; set; }
    }

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.