MVC model validation for email from database in asp.net

There are many cases where we need to validate the value in controls before submitting to the database, so let's discuss email validation like format, length and existence into database. We will walk through in this article, how to validate entered user email from database, whether email is already exists in database if yes then show error to user that email already taken by some one, if no email found in database then allow user to create the account.

To validate the required field in MVC we user

[Required(ErrorMessage = "{0} is required!")]    
[Display(Name = "Email")]
public string Email { get; set; }

if you will try to submit the page without entering any thing it will show the error "Email is required!"

To check email format add this

[DataType(DataType.EmailAddress)]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", 
    ErrorMessage = "Invalid email")]

Some times we need to validate email from database, like when we allow user to create an account only with a valid email id and which is not already taken then there should be some way to validate email from database once user left the email field then we can use following annotation

[Remote("CheckUserName", "User", ErrorMessage = "Already in use")]

Here CheckUserName is the action and User is the controller name

Let's see the full method which can return result in json format

 public JsonResult CheckUserName(string Email)
 {
   MyDataContext db = new MyDataContext();
   var result = true;
   var user = db.Users.Where(x=>x.Email == Email).FirstOrDefault();

   if(user != null)
       result = false;

   return Json(result, JsonRequestBehavior.AllowGet);
 }

That's all we need to set to validate the email from database

Once you will leave the email text box it will validate entered email from database, here is the complete code

    [Required(ErrorMessage = "{0} required")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", 
         ErrorMessage = "Invalid email")]
    [Remote("CheckUserName", "User", ErrorMessage = "Already in use!")]       
    [StringLength(100, ErrorMessage = "{0}: 100 is the limit")]
    public string Email { get; set; }
Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • mvc
  • asp.net
By Ali Adravi On 05 Jan, 13  Viewed: 6,125

Other blogs you may like

mvc search page example with code

MVC Searh page with pagination: It’s very easy to create a search page in asp.net but when I try to create the same in MVC I faced many problems, how to create model, how to keep searched values in search controls, pagination, I found myself nowhere, so start searching for some good examples but... By Ali Adravi   On 25 Aug 2013  Viewed: 36,321

MVC insert update delete and select records

CRUD (Create, Retrieve, Update and Delete) in MVC. When we start to learn new language, first we try to run an application with “Hello World” and then CRUD functionality. So in this article we will see how to select records from database (with WebGrid, pagination and sort functionality), update a... By Ali Adravi   On 17 Aug 2013  Viewed: 99,858

How to create a single thanks page for entire controller in MVC

Sometimes we need a thanks page say we have user registration, change password, activate account functionality in our application then we need a thanks page after registering with our site, to say thanks for registering with us or showing confirmation that your password is successfully changed or... By Hamden   On 30 Jun 2013  Viewed: 3,221

MVC jquery autocomplete with value and text field

In MVC, autocomplete with jquery is the best way to pull data from database and render according to our requirements, so in this article we will use jquery to show the auto complete text box without any ajax controls. We need a method to get the data from database, a controller method to handle the... By Ali Adravi   On 29 Jun 2013  Viewed: 6,295

Upload files with model data in MVC

Upload multiple files with model data in MVC is really very easy, when I started to test by uploading some files, I though it would be more complicated but it is really not. In my previous post [ASP.Net MVC file upload][1], I promised to post soon about how to upload multiple files. When I was... By Ali Adravi   On 04 Jun 2013  Viewed: 23,273