RegularExpression for either valid email or username from the comma separated list

Kuler Master 326 Reputation points
2024-12-30T11:03:03.83+00:00

It's about the login form where some clients must enter their email and only few others their name.

Currently I have the following:

[Required(ErrorMessageResourceName = "Validation_RequiredField", ErrorMessageResourceType = typeof(Resources))]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessageResourceName = "Validation_InvalidUsername", ErrorMessageResourceType = typeof(Resources))]
public string? UserName { get; set; }

Thank you!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,717 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,640 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,158 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kuler Master 326 Reputation points
    2024-12-30T11:17:21.2133333+00:00

    This worked just fine. Thx

    @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*|[a-zA-Z]{4,}"
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.