简体   繁体   中英

Show Modal on form validation

I'm a beginner to javascript and boostrap. What i want is: 1. User clicks the submit button. 2. Validate form using entry_check(). 3. On form validation success. Open bootstrap MODAL with the user input values. The MODAL will act as a Confirmation box with the user values to submit or cancel 4. On click submit button in MODAL POST form.

form

<form role="form" id="ps_entry" name="ps_entry" method="post" action="/user/accounts/ps/add/">
Date<input name="date" id="date" class="form-control" autocomplete="off" />

Rate<input name="rate" id="rate" class="form-control" autocomplete="off" onkeypress="AmtCalc()" />

Box<input name="box" id="box" class="form-control" autocomplete="off" />

Amount<input name="amount" id="amount" class="form-control" readonly />

Payment Date<input name="pdate" id="pdate" class="form-control" autocomplete="off" />

Remarks<textarea name="remarks" id="remarks" class="form-control" autocomplete="off"></textarea></td>

entry_check()

function entry_check(){

    var x = document.forms["ps_entry"]["date"].value;
    if (x == null || x == "") {
        alert("Please select  Date");
        return false;
    }


    var x = document.forms["ps_entry"]["rate"].value;
    if (x == null || x == "") {
        alert("Rate cannot be empty");
        return false;
    }

    var x = document.forms["ps_entry"]["box"].value;
    if (x == null || x == "") {
        alert("No. of Box cannot be empty");
        return false;
    }

}

button

<input type="button" name="submitBtn" value="Submit" id="submitBtn" onclick="submitForm()"  class="btn btn-default" />

onclick submit

$(document).ready(function(){
    $('#submitBtn').click(function(){
    var success = entry_check();
    if (success) {
        $('#confirm-submit').modal('show');


        $('#dat').html($('#date').val());
        $('#rat').html($('#rate').val());
        $('#bo').html($('#box').val());
        $('#amoun').html($('#amount').val());
        $('#pdat').html($('#pdate').val());
        $('#remark').html($('#remarks').val());

        $('#submit').click(function(){
        /* when the submit button in the modal is clicked, submit the form */
            alert('submitting');
        $('#ps_entry').submit();
    });
    }
    });
});

If you want a neater way of validating your form you might want to take a look at jquery validate plugin.

On submit do the following...

var formValidationRules = {
    rules: {
        date: {
            required: true
        },
        rate: {
            required: true
        },
        box: {
            required: true
        },
        (... more form fields)

    }
};

$("#ps_entry").submit(function(){
    $(this).validate(formValidationRules);
});

jQuery Validate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM