views:

37

answers:

2

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.

+1  A: 

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
 } 
Sander Pham
+1  A: 

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).

mdresser
ok can i do it without disrupting my dataannotation validations
mazhar kaunain baig