简体   繁体   中英

Form validation on modal confirm

This is my code. The problem is that the form is submitting when i clicked the confirm button on the modal. The thing is I dont know how to validate the form first before submitting. This is the process I want.
1. The user fill up the field.
2. The user click confirm button (at least here it the form must be validated) or here 3. The modal shows and when I clicked the submit button the modal will close then the form validations will show

 <form role="form" id="formfield" action="<?php echo base_url()?>Shop/add_shop" method="post"    enctype="multipart/form-data">

    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <p><B>Shop name:</b></p>
            <input type="text" class="form-control"  id ="shop_name" name="shop_name">
        </div>
        <div class="col-md-1"></div>
    </div>

<div class="modal fade" id="confirm-submit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Confirm Submit
            </div>
            <form action="<?php echo base_url()?>Shop/add_shop" id="wew" method="post">
            <div class="modal-body">
                Are you sure you want to submit the following details?
                <table class="table">
                    <tr>
                        <th>Shop Name: </th>
                        <td ><input type="text " id="s_name" name="shop_name" readonly></td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button href="#"  onclick="document.getElementById('formfield').submit();" class="btn btn-success success">Submit</button>
            </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('#submitBtn').click(function() {
         $('#s_name').val($('#shop_name').val());
    });

    $('#submit').click(function(){

        $('#formfield').submit();
    });
</script>

You need to handle the form submit event, and do your validation in there.

In the example below, the form will not be submitted unless isValid is true

$("form").submit(function(event) {
  isValid = false; // form is not valid
  if(isValid != true) event.preventDefault(); // prevent the submission
});

You can determine if a field is empty like so:

if($("input[name=shop_name]").val() == '') alert('Shop name cannot be empty!');

There are also really good validation libraries which are pre-built. My personal favorite is validate.js which is available at http://jqueryvalidation.org/ Using a prebuilt library like that one allows you to simply do something like $("form").validate(); and it's very easy to extend with custom rules if you don't like what they have available for use.

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