简体   繁体   中英

How to validate using JavaScript before submitting the form in Zend framework

I wrote this JavaScript code to do validation before submitting the form. Validation is working fine but at the same time its submitting data to the data base. I don't want it to submit, if the condition is wrong.

Here's my code:

<form action="<?= $this->form->getAction() ?>" method="<?= $this->form->getMethod() ?>" class="stdform stdform2" id="product" enctype="multipart/form-data">
                <?= $this->form->employee_id ?>
                <p>
                    <?= $this->form->name ?>
                </p>                            
                <p>
                    <?= $this->form->type ?>
                </p>
                <p>
                    <?= $this->form->brand ?>
                </p>
                <p>
                    <?= $this->form->model ?>
                </p>
                <p>
                    <?= $this->form->condition ?>
                </p>
                <p>
                    <?= $this->form->about ?>
                </p>
                <p>
                    <?= $this->form->listedon ?>
                </p>
                <p>
                    <?php echo $this->form->reserved_price; ?>
                </p>
                <p>
                    <?php echo $this->form->Purchased_price; ?>
                </p>
                <p>
                    <?php echo $this->form->buy_now_price; ?>
                </p>
                <p>
                    <?= $this->form->sale_end_date ?>
                </p>

                <p class="stdformbutton">
                    <?= $this->form->save ?>
                    <?= $this->form->cancel ?>
                </p>    

            </form>                         

and the JS code I've written for it is below:

 <script type="text/javascript">
    $(document).ready(function(){
         // $("#type option[value='null']").attr("disabled","disabled");
var a2=parseInt($('#reserved_price').val());
                    var a3=parseInt($('#Purchased_price').val());
                    var a4=parseInt($('#buy_now_price').val());


$('#product').form2json({success: function() {



                    if(a2 >= a3 && a2 >= a4)
                    {
                        alert("Ensure the minimum price should be lesser than the Purchased and Buy now price");
                    }
                    else

            location.href="/sellproduct/index";

            }
                    });     
        $('#cancel').click(function() {
            location.href="/sellproduct/index";
        });
        $('#listedon').val($.datepicker.formatDate( "yy-mm-dd", new Date()))
        $("#sale_end_date").datepicker( {minDate:0, "dateFormat": 'yy-mm-dd', changeMonth: true, changeYear: true, firstDay: 1 });

    }); 
</script>

Can anyone please help me to write the right code for it?

You will have to put a return false statement below your validation like this,

 if(a2 >= a3 && a2 >= a4)
 {
   alert("Ensure the minimum price should be lesser than the Purchased and Buy now price");
   return false;
 }

Hope this helps!

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