5

I'm using bootstrap form validation, and i want to validate my form using a button that is not inside the form tags.

The reasons i want to do that are:

  1. I don't want to redirect the user to other page or reload the current page, after submiting the form.

  2. I want to call to an ajax call that register a user and return an answer (fail/success).

How can I call the validate form mechanism and getting true or false in order to know if I can redirect to the ajax call.

Thank you.

1
  • 2
    I want , i dont want but where is your code Commented Aug 6, 2015 at 11:16

2 Answers 2

1

Just attach a jquery click event to the button outside the form tags and inside that function do someting along the lines of:

//1) Submit Form using Form's ID
$("#testForm").submit();

//2 Submit Form using Form's Name
$("form[name='myForm']").submit();

//3 Submit Form using Form's Index.
$("form:first").submit();
Sign up to request clarification or add additional context in comments.

3 Comments

That work's. how can i get a value (true/false) if the form validation passed or failed? I tried var form_check = $("#testForm").submit(); ,but i get an object and not a bool variable
Bootstrap is a css framework and dose not have any logic to interact with your back end or JavaScript responses. Just call some custom function on the success method of your ajax call and add the bootstrap classes using jquery addClass(). Here are all the classes: getbootstrap.com/css/#forms-control-validation
Thank you, i solved it using a jquery code: var check_fields = 0 ; $("#RegForm .form-group").each(function(i , value) { if( $(value).attr('class') == "form-group has-feedback has-error"){ check_fields = 1 ; } });
0

You can validate your field value as you want. This is just basic form submission code in jquery.

 $("#yourformid" ).submit(function( event ) {
    var field1 = $('#field1').val();
    var field2 = $('#field2').val();

    var information = {
            'field1' : field1,
            'field2':field2,
    };
    var data = JSON.stringify(information);
    event.preventDefault();

    $.ajax({
    type: "POST",
    data: {value:data},
    url: "YOUR_URL",
    success: function(data) {
        if(data="sucess"){
        console.log(data)
        } else {
        console.log("Mission failed")

     }
    });

});

Comments

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.