Thursday, August 4, 2016

Asp.net mvc check if username exists instantly using remote validation

check that particular username or email address  exists or not in our database instantly at client side . We can achieve particular perspective by hard coding at server side but at client side we can achieve using remote validation attribute in model . Let’s see example .

First create model for user name  using data annotations with remote attribute 

public class RegisterModel
{
        [Remote("CheckUserNameExists", "Common")]
        [Required(ErrorMessage="User name is required")]
        [Display(Name = "User Name")]
        public string UserName { get; set; }
}

Here we are using remote attribute to check instantly that user name  exists or not . If we check parameters of remote attribute , we will find that we need to provide controller and action name . We provided Controller name as Common and action name as CheckUserNameExists . Let’s create action name as “CheckUserNameExists” .

Create action for remote validation

public virtual JsonResult CheckUserNameExists(string userName)
{
    bool chkUser = false;
    //Check in database that particular user name is exist or not
    chkUser = CheckUserNameExistsFunction(userName);
    if (chkUser)
    {
         return Json("User name is already registered ", JsonRequestBehavior.AllowGet);
    }
    else
    {
         return Json(true, JsonRequestBehavior.AllowGet);
    }
}
By calling above method we can check that user name  exists or not instantly as it  returns Json Result . Now we we will see how we can implement at view side where it will show the message instantly .
@Html.TextBoxFor(m =>m.UserName, new { placeholder = "Enter user name", id = "txtUserName" })
@Html.ValidationMessageFor(m =>m.UserName, string.Empty, new { @class = "error-class" })

What is your opinion ?

By implementing above all things we can achieve instantly message that user name is already exist or not . I hope now you can easily implement remote validation . If you have any query you can comment or you can mail me.
Nice one, thanks for that Dilip Patel. @ahmet and Dilip, you could rewrite your function a little to simplify things, like so:
[AllowAnonymous]
        public virtual JsonResult CheckUserNameExists(string userName)
        {
            //Check in database via Usermanager that particular user name is exist or not
            bool userExists = UserManager.Users.Any(u => u.UserName == userName);
            return userExists
                ? Json(Help.User_name_already_registred, JsonRequestBehavior.AllowGet)
                : Json(true, JsonRequestBehavior.AllowGet);
        }
        [AllowAnonymous]
        public virtual JsonResult CheckEmailExists(string email)
        {
            //Check in database via Usermanager that particular email is exist or not
            bool userExists = UserManager.Users.Any(u => u.Email == email);
            return userExists
                ? Json(Help.Email_already_registred, JsonRequestBehavior.AllowGet)
                : Json(true, JsonRequestBehavior.AllowGet);
        }