How to set [EmailAddress] for UserName. Validation in the registration form does not work.

Volk Volk 571 Reputation points
2022-07-08T13:27:22.897+00:00

Hello!

I am creating a registration form for a website. Net Core 6. UserName is set in my Email field. How do I make it so that I set Email verification (RegExp) on this field. Everything works without this check, but I don't need the user to enter any strings there and fill my database with incorrect values.

I have an ApplicationUser model for this form and I have tried different options, but I always get this message: Username '' is invalid, can only contain letters or digits.

Of course, I can do this check in the code in the public async Task<IActionResult> Create(Application User user) method in the UserController controller. But this is wrong. I want to understand where to set RegExp for this field correctly with Net Core Functions?

Thank you!

using Microsoft.AspNetCore.Identity;  
using System.ComponentModel.DataAnnotations;  
  
namespace Project.Models  
{  
    public class ApplicationUser : IdentityUser  
    {          
        [Required]  
        [RegularExpression(@"^[a-zA-Z0-9]{3,15}", ErrorMessage = "Incorrect login characters.")]  
        public string Login { get; set; }          
  
        [Required]  
        [EmailAddress]  
        public string UserName { get; set; }  
  
        [Required]  
        public string FirstName { get; set; }  
  
        [Required]  
        public string LastName { get; set; }  
    }  
}  

218964-a.png

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,166 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,281 Reputation points
    2022-07-08T14:02:42.39+00:00

    I'm a little confused as to your goal. Your form requires a login (user name) and email address. I don't know what you mean by "user name is set in my email field". In your UI they are 2 separate fields and aren't the same.

    Are you trying to make the login name also an email address? If so then just apply the EmailAddressAttribute to the field and remove the regular expression. However note that your identity system (which you didn't provide the configuration for) may have different requirements and therefore you need to ensure your identity system allows user names to be in an email format.

    Of course it doesn't make sense, to me, to ask the user to enter a login name as an email address + an email address so I'd say get rid of the email address altogether in the UI and have your creation method set them both to the same value. But you really need to be careful here as people's email change over time. If they change emails later then you probably don't want them to have to create a new account so you'd need to also support changing user names.


0 additional answers

Sort by: Most helpful