maztt
maztt

Reputation: 12294

asp.net mvc datannotation unique field

i followed http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx . it was fine till server side but how would i be applying client side validation for unique field validation scenario on for example say username. i want to have username as unique.

Upvotes: 0

Views: 402

Answers (2)

Matthew Dresser
Matthew Dresser

Reputation: 11442

You couldn't really expect to do client-side validation to ensure that a supplied username is unique (not in the same way you can validate that they've entered an integer into a textbox). You'd have to do a postback to check the username server-side against your database. If you wanted to avoid the postback, you could use Ajax to check the username against the database after the user has typed their username (e.g. when the textbox looses focus).

Upvotes: 1

S P
S P

Reputation: 4643

From your ASP.NET MVC Pages you can refer to your service layer as follow:

  <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
     <asp:ServiceReference Path="UserService.svc" />
    </Services>
  </asp:ScriptManager>

Then, in order to do something with it:

function ValidateUsername() {
   var username = $get("tbUsername").value;
   MyNamespace.UserService.ValidateUser(username, OnComplete);
 }

 function OnComplete(results) {
    // Your result handling
 } 

Upvotes: 1

Related Questions