Search code examples
jqueryjquery-validation-engine

How to combine 2 click function?


I have form that sends message me when enter Email and MessageText. I want to use both of jQuery Validation Engine plugin and ajax response to return result. For this I use same jQuery('#messageSendButton').click() function in every case.

To return response from controller:

<script type="text/javascript">
                jQuery('#messageSendButton').click(function () {

                    var url = '@Url.Action("SendQuickMessage", "Home")';
                    var email = $('#email').val();
                    var messageText = $('#messageText').val();

                    $.ajax({
                        url: url,
                        type: 'GET',
                        contentType: 'application/json',
                        data: { Email: email, Message: messageText },
                        success: function (result) {
                            if (result.success) {
                                alert("Message sent! Thanks!");
                            }
                            else {
                                alert("Error occured!");
                            }
                        },

                        complete: function () {
                            $(".loading").hide();
                        }
                    });
                });
            </script>

And, to validate form:

  <script type="text/javascript">
                jQuery(document).ready(function () {
                    jQuery('#quickMessageForm').validationEngine('init');
                    jQuery('#messageSendButton').click(function () {
                        jQuery('#quickMessageForm .loading').animate({ opacity: 1 }, 250);
                        if (jQuery('#quickMessageForm').validationEngine('validate')) {

                            jQuery.post('/Home/SendQuickMessage', {
                                Email: jQuery('#email').val(),
                                Message: jQuery('#message').val(),
                                formname: 'messageForm',
                                formtype: 'messageF'
                            }, function () {
                                jQuery('#quickMessageForm .loading').hide();
                            });

                            return false;
                        } else {
                         jQuery('#quickMessageForm .loading').animate({ opacity: 0 }, 250);
                            return false;
                        }
                    });
                });
            </script>

Separately, they both works alone, but together, firstly ajax function works, second validation works. it is not right.. How can I combine those in one? Firstly to validate form, then get ajax response.

Edit:

Here is form:

 <form action="#" method="post" id="quickMessageForm">
            <div class="form_info cmsms_input">
                <label>Email<span class="color_3"> *</span></label>
                @Html.TextBoxFor(model => model.Email, new { name = "email", id = "email", tabidex = 1, @class = "validate[required,custom[email]]" })
            </div>
            <div class="form_info cmsms_textarea">
                <label>Message<span class="color_3"> *</span></label>
                @Html.TextAreaFor(model => model.Message, new { id = "messageText", name = "messageText", tabindex = 2, @class = "validate[required,minSize[10]]" })
            </div>
            <div class="loading"></div>
            <div>
                <input type="submit" name="messageSendButton" id="messageSendButton" value="Send" class="button_small" tabindex="3" />
            </div>
        </form>

Solution

  • You can try like below.

    i.e. make your ajax call async:false

    $.ajax({
    url: restmethod,
    type: "POST",
    contentType: "application/json",
    async:false
    success: function(data) {
        return data.d; 
    }
    });
    

    I hope this will help to you.