繁体   English   中英

jQuery 验证不适用于 cakephp 表单元素

[英]jQuery validation not working on cakephp form elment

我在 cakephp 表单元素上使用 jQuery 验证。 第一个名称标签 jQuery 对键起作用,但对检测其长度不起作用。 表单提交 function 似乎根本不起作用,我通过检查器/控制台没有看到错误..

   <span class="text-center text-danger" id="error"></span>

   <?= '<div class="col-md-6 form-group"><label>First Name<span class="text-danger">*</span></label>'.
                            $this->Form->control('Tutors.first_name',['label' => false,'id' => 'first_name','class' => 'form-control' ]).'</div>'; ?>

   <?= $this->Form->button(__('Submit'),['class'=>'btn btn-success']) ?>

   <script src="https://code.jquery.com/jquery-3.6.0.min.js" ></script>
   <script type="text/javascript">
   // Document is ready
   //$(document).ready(function () {
    
    
    $('#tutors-last-name,#first_name').keyup(function () { 
    this.value = this.value.replace(/[^A-Za-z\.]/g,'');
    });
    
  $('form').submit(function() {   //this doesnt work
      
    $("#error").text("");
    //prevent Default functionality
    var minLength = 2;
    var maxLength = 10;

    if ($('#first_name').val().length < minLength){
     
       $("#error").text("Minimum length is 2 chars for First name ");
       $('html, body').animate({ scrollTop: $("form").offset().top    }, 2000);
       return false;
      
    } 
    
    }); 
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM