1

I'm trying to validate my form using jQuery validate engine before sending the ajax request. But if form contains the error also ajax sends the request.

JavaScript:

$('#addphrase').validationEngine('attach', {
    inlineValidation: false,
    promptPosition: "centerRight",
    onSuccess: function () {
        use_ajax = true;
        alert(dcd);
    },
    onFailure: function () {
        use_ajax = false;
        alert(ggg);
    }
});

$('form[name="addphrase"]').submit(function (e) {
    if (use_ajax) {
        $('#loading1').css('visibility', 'visible');
        $.get('addphrase.php', $(this).serialize() + '&ajax=1',

        function (data) {
            if (parseInt(data) == -1) {
                $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">* Please ensure all fields are filled.","error"</p>');
            } else {
                $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">Added successfully. Thank You...</p>');
            }
            $('#loading1').css('visibility', 'hidden');
            setTimeout("jQuery('#resp1').hide('slow');", 3000);
            setTimeout("jQuery('#resp-mes1').hide('slow');", 5000);
        });
    }
    e.preventDefault();
}
});

Edit HTML

  <form class="form-horizontal" id="addphrase" name="addphrase" method="GET" action="">
            <div class="control-group"><label class="control-label" for="form-field-1">Phrase</label>
  <div class="controls">
  <input type="text" id="phrase" class="validate[required]" placeholder="Phrase" name="Phrase" value="" /></div></div>
  <div class="control-group"><label class="control-label" for="form-field-11">Content Here</label>
  <div class="controls">
  <textarea name="post_content"  value="" class="validate[required] autosize-transition span12" id="comment" style="overflow: hidden; word-wrap: break-word; resize: horizontal; height: 67px;"></textarea></div></div>
  <div class="space-4"></div><div class="control-group" style="float:left; margin-right:25px"><div class="controls">
  <button type="submit" class="btn btn-info">
 <i class="icon-ok bigger-110"></i>
 <input type="submit" value="" id="phsubmit" style="opacity:0">Submit</button>
 <button type="reset" class="btn"><i class="icon-undo bigger-110"></i>Reset</button></div></div>
 <div id="resp1" style="float:left; margin-top:5px"><img id="loading1" style="visibility:hidden;" src="assets/img/ajax-load.gif" width="16" height="16" alt="loading" /></div></div>
</form>

In above code it won't show alert and it won't attach the property,in both case if it is failure or success.

Any help would be appreciated.

2
  • Is you form name and id are same? Put your html code Commented Oct 17, 2013 at 5:57
  • @Tushar Gupta, @ Ajith S Added Html form Commented Oct 17, 2013 at 6:02

1 Answer 1

1

Try to use onValidationComplete like,

$('#addphrase').validationEngine('attach', {
    inlineValidation: false,
    promptPosition: "centerRight",
    onValidationComplete: function (form, status) {
        if (status) {
            alert('Form submit');
            $('#loading1').css('visibility', 'visible');
            $.get('addphrase.php', $(this).serialize() + '&ajax=1',

            function (data) {
                if (parseInt(data) == -1) {
                    $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">* Please ensure all fields are filled.","error"</p>');
                } else {
                    $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">Added successfully. Thank You...</p>');
                }
                $('#loading1').css('visibility', 'hidden');
                setTimeout("jQuery('#resp1').hide('slow');", 3000);
                setTimeout("jQuery('#resp-mes1').hide('slow');", 5000);
            });
        }
        else
        {
            alert('Error');
        }
    },
});

Read Docs

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

What happening here is promptPosition: "centerRight", onSuccess: function () { use_ajax = true; alert(dcd); $('#addphrase').submit();// submit form here }, onFailure: function () { use_ajax = false; alert(ggg); } These line are not executing at all. Please tell me is any mistake have in those lines

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.