Home > ASP.Net, Javascript, Jquery > Client Side Validation with CustomValidator using JQuery

Client Side Validation with CustomValidator using JQuery


Hi all,

try this example for Client Side Validation with CustomValidator using JQuery

1) Open VS2008 and create a new website and add new web page

2) From Toolbox drag Text-box and CustomValidator controls


Enter Code :
 <asp:TextBox ID="TextBoxCode" runat="server" Width="195px"></asp:TextBox>
 <asp:CustomValidator ID="CustomValidator6" runat="server" ControlToValidate="TextBoxCode"
 ErrorMessage="This code already exists Please enter another one."
 ForeColor="Red" ValidationGroup="InsertUpdate"
 ClientValidationFunction="CheckUniqueness"></asp:CustomValidator>
 <br />
 <asp:Button ID="Button1" runat="server" Text="Save" ValidationGroup="InsertUpdate"/>

3) In code behind add the following code


[WebMethod]
 [ScriptMethod]
 public static bool IsUnique(string code)
 {
 return false;
 }

4) Add this JavaScript to validate CustomValidator using Client Side


<script type="text/javascript">
 function CheckUniqueness(src, args) {
 var isValid;
 $.ajax({
 type: "POST",
 url: "Default.aspx/IsUnique",
 data: "{'code': '" + args + "'}",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 async: false,
 success: function(msg) {
 isValid = msg.d;
 }
 });
 args.IsValid = isValid;
 }
 </script>

after that run your page

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net, Javascript, Jquery
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: