0

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(); ?>
0

4 Answers 4

3

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

Returning false after failed validation is enough.

Sign up to request clarification or add additional context in comments.

2 Comments

if i don't call event.preventDefault(); then this function $('form[name=contact_form]').submit(function(event) is not calling form directly submit..
It looks like there should be return false in if (phone==0) part too. You are returning false when phone is shorter than 10 chars, but not when it has length equal to 0.
0
$(this).submit();

When it's valid.

Comments

0

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

Comments

0

I usually do something like

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

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.