I have a Model
which have both phone number and email in MVC 4
. It's something like registration. So user can provide either email or cellphone number or both of them. However I can't make a alternative required field of them.
My Model is like
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
[UsernameValidator(ErrorMessage = "Only Letters and numbers")]
[System.Web.Mvc.Remote("IsUserExitst", "Account", HttpMethod = "POST", ErrorMessage = "This user name already exists. Try a new one.")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[MaxLength(50)]
[EmailOrPhoneValidator(ErrorMessage = "Invalid Phone number")]
[Display(Name = "Phone number")]
[System.Web.Mvc.Remote("IsMobOrEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists. Try a new one.")]
public string MobileNo { get; set; }
[Required]
[MaxLength(50)]
[EmailAddress(ErrorMessage = "Invalid Email address")]
[Display(Name = "Email")]
[System.Web.Mvc.Remote("IsEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Email already exists. Try a new one.")]
public string Email { get; set; }
}
Here I want to use Email
and PhoneNo
alternatively. I want to write a class which works as Custom required field. I have gone through this Questions,
- Question Here the answer provided a solution combined with javascript. But I won't use it. I will write down the class file and implement my logics here.
RequiredIf
is seems the exact suggestion of my question, but I so tired to try that solution. I am not capable to understand how to import the .dll in my project. and it's developed in Mvc 3- Another solution, but here they have combined the alternative properties in a class. So how do I do as my properties are completely separate. So what to do.