When we create Registration form on a web application, we have to check that the email address and username that user is entering is unique and is not already taken by another user. In ASP.NET MVC we have Validation Attribute feature under System.ComponentModel.DataAnnotations and System.Web.Mvc which we can use for different type of Validations before sending data to be saved in persistent location.
In System.Web.Mvc we have Remote attribute which is available in ASP.NET MVC 4 in which I am implementing it for this post. I am not sure if it is available in the earlier version of ASP.NET MVC.
So I have this ViewModel:
- public class SignUpViewModel
- {
-
- public int UserID
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "First Name is required")]
- public string FirstName
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Last Name is Required")]
- public string LastName
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Username is Required")]
- [RegularExpression(@ "^[a-zA-Z0-9]+$", ErrorMessage = "user name must be combination of letters and numbers only.")]
- public string UserName
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Password is Required")]
- public string Password
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Password is Required")]
- [System.Web.Mvc.Compare("Password", ErrorMessage = "Both Password fields must match.")]
- public string ConfirmPassword
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Email Address is required")]
- [EmailAddress(ErrorMessage = "Invalid Email Address")]
- public string Email
- {
- get;
- set;
- }
As you can see there are already attributes on the properties of ViewModel like Required which are mandatory fields, RegularExpression attribute for Email Address format validation and Compare attribute for comparing two properties values which is useful here for Password and Repeat Password textbox for making sure that user has verified what password he is setting.
Now what will happen if user enters a username or Email Address which is already registered, one way is to check in post action for username and Email Address and show user error message that username or Email Address has already taken which does not look though, as validation should be done before posting data to action using unobtrusive validation.
Now here we will use Remote attribute which will check for both UserName and Email Address field, here is how it will be done:
- [Required(ErrorMessage = "Username is Required")]
- [RegularExpression(@ "^[a-zA-Z0-9]+$", ErrorMessage = "user name must be combination of letters and numbers only.")]
- [Remote("UsernameExists", "Account", HttpMethod = "POST", ErrorMessage = "User name already registered.")]
- public string UserName
- {
- get;
- set;
- }
-
- [Required(ErrorMessage = "Email Address is required")]
- [EmailAddress(ErrorMessage = "Invalid Email Address")]
- [Remote("EmailExists", "Account", HttpMethod = "POST", ErrorMessage = "Email address already registered.")]
- public string Email
- {
- get;
- set;
- }
Remote Attribute has different parameters to be specified:
- Action Name which will be called which will have input parameter for this property value.
- Controller Name of which action will be executed.
- HttpMethod (Get or Post).
- ErrorMessage (Error Message that will be displayed if validation fails).
Let's come to the action part we need to write action method in the Account controller for Validation, for this post just for readers understanding I am just checking specific username and email (my name and dummy email address), but in your project you will need to check in your database for that particular criteria and return true or false respectively.
Here is the action code:
- public JsonResult UsernameExists(string username)
- {
- return Json(!String.Equals(username, "ehsansajjad", StringComparison.OrdinalIgnoreCase));
- }
-
- public JsonResult EmailExists(string email)
- {
- return Json(!String.Equals(email, "[email protected]", StringComparison.OrdinalIgnoreCase));
- }
Here is the complete View code Now when I will enter username "ehsansajjad" or Email Address "
[email protected]" validation will get fired that user already exists:
You can download the sample project
here.