简体   繁体   中英

How to submit form if validate using jquery?

I am validating form with jquery. So when form submit i am using event.preventDefault(); after validate form , form will be submit to respective action page.But in my case it doesn't work.

Code what i did-

$('form[name=contact_form]').submit(function(event) {
        event.preventDefault();
        var formData = $('form[name=contact_form]').serialize();
        var phone = $.trim($('form[name=contact_form]').find('input[name=PHONE]').val()).length;
        var intRegex = /[0-9 -()+]+$/;
        var alert = $('#contact_alert');
        var success = $('#contact_success');
        if(phone==0){
            phone.focus();
            alert.html("<p class='alert alert-danger' style='text-align:left;'>Phone number is required</p>");
        }
        else if((phone < 10)){
            phone.focus();
             alert.html("<p class='alert alert-danger' style='text-align:left;'>Please enter a valid phone number</p>");
             return false;
        }else{
            return true;
        }
    });

Html Page :

<?php
echo form_open_multipart('home/inquiry', array('name' => 'contact_form'));
?>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="hidden" name="form_type" value="C">
                            <input type="text" name="NAME" id="user-name" class="form-control" placeholder="Full Name"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="text" name="PHONE" id="user-phone" class="form-control" placeholder="Phone"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="text" name="EMAIL" id="user-email" class="form-control" placeholder="Email"/>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <input type="text" name="SUBJECT" id="user-subject" class="form-control" placeholder="Reason for inquiry"/>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <textarea class="form-control" name="MESSAGE" id="user-message"  placeholder="Message"></textarea>
                    </div>
                    <div class="col-md-12 text-left">
                        <div class="form-group">
                            <input type="submit" name="submit"  class="btn btn-warning">
                        </div>
                    </div>
                </div>
                <?php echo form_close(); ?>

Do not call event.preventDefault(); if you want the form to be submitted after it validates ok.

Returning false after failed validation is enough.

$(this).submit();

有效时。

Try to replace the return true; for $(this).submit();

I usually do something like

$("#submit").submit(function(e){
    if(validateFields() == true){ //If all fields are valid
        $("#submit").submit();
    }
    else{
        e.preventDefault(e);
    }    
});

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