Data Haunting

Easy Solutions Destination

By mvc check if username exists instantly using remote validation

In mvc many times we want to 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

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

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 .

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.

10 Responses to mvc check if username exists instantly using remote validation

  1. ahmet says:

    where is “CheckUserNameExistsFunction” how can i write it?

    • Dilip Patel says:

      Hi it is just example that you have to write sql function that is named here “CheckUserNameExistsFunction” by using sql query in your database and need to check whether user is actually already registered or not ?

  2. Nice one, thanks for that Dilip Patel. @ahmet and Dilip, you could rewrite your function a little to simplify things, like so:

    As you can see, I am instead using the Usermanager (I assume you are using the AccountController in which the Usermanager already is instantiated to place these functions in) to check the existence of username and email to simplify things. I also added localization (needs a resource named or whatever) and did the same for e-mail.

  3. Rahul says:

    I need a resource named or whatever. how do i make it?

  4. Adam Jungers says:

    How do I call this sql function so it will show up in my controller? Because I wrote the function in my sql database, but I am unable to connect to it in my controller.

    • Dilip Patel says:

      Hi Adam
      Here as I have mention Function name CheckUserNameExists(userName) and passed argument “username” that is our class method or function name. From that class method or function you can use Sql Query or Stored Procedure to check whether username or email address is already exists or not. You can not use Sql server’s function directly here. You can also use Sql Query or Stored Procedure from your controller. In comment christianschiffernorway created method “CheckUSerNameExist” and “CheckEmailExists” and using linq query he check whether username or email exists or not and than he returns JsonResult. You can use return type boolean also as our requirements fit here.I hope now you can understand.

Leave a Reply