views:

28

answers:

1

my target is to create form that validated in the client side, and only when it is valid, send ajax call to asmx web service. i manage to do that two separately: client-side validation and ajax send to web service, and i want to combine this two. how?..
i have this form (i simplify everything for simple example):

<form id="ContactForm" runat="server">
   <label for="name">Full Name</label>
   <input type="text" name="name" id="name"/>
   <input id="submit"  type="button" />
</form>

the client validation looks like:

    $(document).ready(function() {
        $("#submit").click(function() {
            var validator = $("#ContactForm").validate({
                rules: { name: { required: true } },
                messages: { name: errName }
            }).form();
        });
    });

and the ajax send looks like this:

 $(document).ready(function() {
        $("#submit").click(function() {
          var myMailerRequest = {name: $('#name').val()};
          var data = JSON.stringify({req: myMailerRequest}); 
                $.ajax
                ({
                    type: "POST",
                    url: "ContactFormMailer.asmx/SendContactForm",                
                    data:  data,                 
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {                           
                    AjaxSucceeded(msg);                            
                 }, error: AjaxFailed
                });
        });
  });

Thanks!

A: 

You can use the submitHandler option of .valdiate() for this, it only executes when a valid form is submitted (it has an invalidHandler for the opposite), like this:

$(function() {
  $("#submit").click(function() {
    var validator = $("#ContactForm").validate({
       rules: { name: { required: true } },
       messages: { name: errName },
       submitHandler: function() {
         var myMailerRequest = {name: $('#name').val()};
         var data = JSON.stringify({req: myMailerRequest}); 
         $.ajax({
                type: "POST",
                url: "ContactFormMailer.asmx/SendContactForm",                
                data:  data,                 
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
         });
       }    
    }).form();
  });
});

Since you're not using this though, it might be much more readable in 2 functions, like this:

$(function() {
  $("#submit").click(function() {
    var validator = $("#ContactForm").validate({
      rules: { name: { required: true } },
      messages: { name: errName },
      submitHandler: ajaxSubmit
    }).form();
  });
  function ajaxSubmit() {
    var myMailerRequest = {name: $('#name').val()};
    var data = JSON.stringify({req: myMailerRequest}); 
    $.ajax({
      type: "POST",
      url: "ContactFormMailer.asmx/SendContactForm",                
      data:  data,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: AjaxSucceeded,
      error: AjaxFailed
    });
  }
});

The only other change was shortening your call to AjaxSuceeded (maybe this can't be done, only because of your simplified example), but other than that...just submit the form from the submitHandler callback and you're all set.

Nick Craver
thanks nick, but still i have one problem:when the submit type=button (like in these examples), the client validation working, but when valid, the submitHandler not executed at all. when i chanege the submit to type=submit, the client validation working, and when valid, the submitHandler executed, but from some reason the return content type from the webservice is text\html and not json as expected (tested with HttpFox).how can i solve this problem or alternatively, how can i invoke the submitHandler from button type?Thanks!
eran
@eran - If you just have `$('input[type=button]').click(function() { $(this).closest('form').submit(); });` it should work...anything that invokes the submit handler.
Nick Craver
@nick - o.k step by step.. now the submitHandler working with type=button so now i have one last problem - the data from the web service received as text/html and not as json. everythings works fine, but still, i will be happy to get the data as expected (when i'm execute the code without validation i get it as json).and again, Thanks!
eran
@eran - What web service? :) Can you post the code calling this? The `contentType` and `dataType` have to be set correctly, like this: `contentType: "application/json; charset=utf-8", dataType: 'json'`
Nick Craver
@nick everything is working now!! the problem was that i had spell error in the webservice name (you can look at the call at my first post in this page - ContactFormMailer.asmx).Thanks you are great!
eran
@eran - Welcome :)
Nick Craver