ASP.NET MVC Data Annotation Verifier for Email or Phone

I am working on registering a form for ApplicationUser . There I have an Email or phone field like Facebook. I am using DataAnnotation to validate the model. In the Data annotation I get [EmailAddress] for email authentication and [Phone] for phone number verification. But I need something like [EmailAddressOrPhone] . So how can I achieve this?

 public class RegisterViewModel { ..... [Required] [Display(Name = "Email or Phone")] public string EmailOrPhone { get; set; } ...... } 
+8
c # regex validation asp.net-mvc data-annotations
source share
3 answers

You can achieve this using Regex . You must combine the regular expression for email and phone together.

 public class RegisterViewModel { [Required] [Display(Name = "Email or Phone")] /* /<email-pattern>|<phone-pattern>/ */ [RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$|^\+?\d{0,2}\-?\d{4,5}\-?\d{5,6}", ErrorMessage = "Please enter a valid email address or phone number")] public string EmailOrPhone { get; set; } } 

Or you can create a custom attribute

  public class EmailOrPhoneAttribute : RegularExpressionAttribute { public EmailOrPhoneAttribute() : base(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$|^\+?\d{0,2}\-?\d{4,5}\-?\d{5,6}") { ErrorMessage = "Please provide a valid email address or phone number"; } } 

and use it

  public class RegisterViewModel { [Required] [Display(Name = "Email or Phone")] [EmailOrPhone] public string EmailOrPhone { get; set; } } 
+2
source share

You should not use your own regular expression to check email addresses , however I would personally use the following logic to determine if it is a valid email address. If this is an invalid email address, you can consider it a phone number.

The following code uses the [EmailAddress] attribute that you already mentioned, but inside the code. This way, you don’t have to invent your own way to verify your email address.

 public EmailOrPhoneAttribute : ValidationAttribute { public override IsValid(object value) { var emailOrPhone = value as string; // Is this a valid email address? if (this.IsValidEmailAddress(emailOrPhone)) { // Is valid email address return true; } else if (this.IsValidPhoneNumber(emailOrPhone)) { // Assume phone number return true; } // Not valid email address or phone return false; } private bool IsValidEmailAddress(string emailToValidate) { // Get instance of MVC email validation attribute var emailAttribute = new EmailAddressAttribute(); return emailAttribute.IsValid(emailOrPhone); } private bool IsValidPhoneNumber(string phoneNumberToValidate) { // Regualr expression from /questions/721154/c-regex-to-validate-phone-number/2731024#2731024 for phone numbers var regex = new Regex("^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$"); return regex.IsMatch(phoneNumberToValidate) } } 

Naturally, you will need to determine how you are going to determine if the phone number is correct, because the phone numbers are different in different countries. I just made a regular expression from this question as an example.

+3
source share

In addition to custom attributes, you can take a look at this library: https://github.com/jwaliszko/ExpressiveAnnotations

Example:

 [Required] [AssertThat("IsEmail(EmailOrPhone) || (Length(EmailOrPhone) > 8 && Length(EmailOrPhone) < 16 && IsRegexMatch(EmailOrPhone, '^\\d+$'))", ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.EmailFormatInvalid))] [Display(Name = "EmailOrPhone")] public string EmailOrPhone { get; set; } 

where I used the AssertThat attribute from ExpressiveAnnotations and created the EmailFormatInvalid error message in the Resources file.

+1
source share

All Articles