jquery - Form validation before submitting

I have a form which converts to pdf on submit. I have added some validation to the form but it does not seem to work.I h

I have a form which converts to pdf on submit. I have added some validation to the form but it does not seem to work.

I have tested in Codepen and JSfiddle and the code works fine but on Wordpress it skips the validation and jumps straight to submit

Code:

<div id="contact-form">
    <form  method="post" action="/">
        <div class="podrecieved">
            <h3>Recieved by<h3>
        </div>
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" name="name">
    </div>
    <div class="form-group">
      <label for="email">Email address:</label>
      <input type="email" class="form-control" id="email" name="email">
    </div>
        <div>                 
            <label for="message">
                <span class="required">Additional Comments:</span> 
                <textarea id="message" name="message" placeholder="Please write your message here." tabindex="5"></textarea> 
            </label>  
        </div>     
        <button name="submit" type="submit" id="submit">Download PDF</button>
    </form>
</div>

JS:

<script src=".1.1/jquery.min.js"></script>
<script src=".12.1/jquery-ui.min.js"></script>
<script src=".4.1/html2canvas.min.js"></script>
<script src=".3.3/jspdf.min.js"></script>
<script src=".validation/1.15.0/jquery.validate.min.js"></script>
<script src=".3.6/js/bootstrap.min.js"></script>

var $form = $("form"),
$successMsg = $(".alert");
$.validator.addMethod("letters", function(value, element) {
  return this.optional(element) || value == value.match(/^[a-zA-Z\s]*$/);
});
$form.validate({
  rules: {
    name: {
      required: true,
      minlength: 3,
      letters: true
    },
    email: {
      required: true,
      email: true
    }
  },
  messages: {
    name: "Please specify your name (only letters and spaces are allowed)",
    email: "Please specify a valid email address"
  },
  submitHandler: function() {
    $ = jQuery;

    $( "#submit" ).click(function() {
    make_product_sheet();
});


function make_product_sheet() {

    console.log("#submit clicked");
    var pdf = new jsPDF('p', 'pt', 'a4');
    pdf.addHTML(document.getElementById("product_sheet"), function() {

        ps_filename = "generated-product-sheet";
        pdf.save(ps_filename+'.pdf');
    });
}
  }
});

as mentioned, works fine on codepen and JSFiddle but when running on my site, it skips the validation and goes straight to the submit (download).

I have a form which converts to pdf on submit. I have added some validation to the form but it does not seem to work.

I have tested in Codepen and JSfiddle and the code works fine but on Wordpress it skips the validation and jumps straight to submit

Code:

<div id="contact-form">
    <form  method="post" action="/">
        <div class="podrecieved">
            <h3>Recieved by<h3>
        </div>
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" id="name" name="name">
    </div>
    <div class="form-group">
      <label for="email">Email address:</label>
      <input type="email" class="form-control" id="email" name="email">
    </div>
        <div>                 
            <label for="message">
                <span class="required">Additional Comments:</span> 
                <textarea id="message" name="message" placeholder="Please write your message here." tabindex="5"></textarea> 
            </label>  
        </div>     
        <button name="submit" type="submit" id="submit">Download PDF</button>
    </form>
</div>

JS:

<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>
<script src="https://cdn.jsdelivr/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn/bootstrap/3.3.6/js/bootstrap.min.js"></script>

var $form = $("form"),
$successMsg = $(".alert");
$.validator.addMethod("letters", function(value, element) {
  return this.optional(element) || value == value.match(/^[a-zA-Z\s]*$/);
});
$form.validate({
  rules: {
    name: {
      required: true,
      minlength: 3,
      letters: true
    },
    email: {
      required: true,
      email: true
    }
  },
  messages: {
    name: "Please specify your name (only letters and spaces are allowed)",
    email: "Please specify a valid email address"
  },
  submitHandler: function() {
    $ = jQuery;

    $( "#submit" ).click(function() {
    make_product_sheet();
});


function make_product_sheet() {

    console.log("#submit clicked");
    var pdf = new jsPDF('p', 'pt', 'a4');
    pdf.addHTML(document.getElementById("product_sheet"), function() {

        ps_filename = "generated-product-sheet";
        pdf.save(ps_filename+'.pdf');
    });
}
  }
});

as mentioned, works fine on codepen and JSFiddle but when running on my site, it skips the validation and goes straight to the submit (download).

Share Improve this question asked Jun 25, 2019 at 10:10 Paulmcf1987Paulmcf1987 312 silver badges9 bronze badges 2
  • did you check your web browser console? are you getting any javascript/jquery errors in console ? – Chetan Vaghela Commented Jun 25, 2019 at 10:15
  • @ChetanVaghela no errors. console returns - #submit clicked – Paulmcf1987 Commented Jun 25, 2019 at 10:27
Add a comment  | 

1 Answer 1

Reset to default 0

Please Try bellow code. i have made some changes in your code.it seems now working to me. i have changes fields name of forms and little bit changes in jquery. please check your end and let me know if your issue will solve.

HTML

 <div id="contact-form">
    <form  method="post" action="/" name="pdf-download">
        <div class="podrecieved">
            <h3>Recieved by<h3>
        </div>
    <div class="form-group">
      <label for="firstname">Name:</label>
      <input type="text" class="form-control" id="firstname" name="firstname">
    </div>
    <div class="form-group">
      <label for="email_id">Email address:</label>
      <input type="email_id" class="form-control" id="email_id" name="email_id">
    </div>
        <div>                 
            <label for="messages">
                <span class="required">Additional Comments:</span> 
                <textarea id="messages" name="messages" placeholder="Please write your message here." tabindex="5"></textarea> 
            </label>  
        </div>     
        <button name="submit" type="submit" id="submit">Download PDF</button>
    </form>
</div>

JS

<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
<script src="https://cdnjs.cloudflare/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>
<script src="https://cdn.jsdelivr/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://maxcdn.bootstrapcdn/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
jQuery(function($){

      var $form =  $("form[name='pdf-download']"),
        $successMsg = $(".alert");
        $.validator.addMethod("letters", function(value, element) {
          return this.optional(element) || value == value.match(/^[a-zA-Z\s]*$/);
        });
        $form.validate({
          rules: {
            firstname: {
              required: true,
              minlength: 3,
              letters: true
            },
            email_id: {
              required: true,
              email: true
            }
          },
          messagess: {
            firstname: "Please specify your name (only letters and spaces are allowed)",
            email_id: "Please specify a valid email address"
          },
          submitHandler: function() {
            $ = jQuery;

            $( "#submit" ).click(function() {
                alert("clicked");
            make_product_sheet();
        });


        function make_product_sheet() {

            console.log("#submit clicked");
            var pdf = new jsPDF('p', 'pt', 'a4');
            pdf.addHTML(document.getElementById("product_sheet"), function() {

                ps_filename = "generated-product-sheet";
                pdf.save(ps_filename+'.pdf');
            });
        }
          }
        });

});
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371669a4624822.html

相关推荐

  • jquery - Form validation before submitting

    I have a form which converts to pdf on submit. I have added some validation to the form but it does not seem to work.I h

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信