Jquery验证表未发布

时间:2016-05-04 23:17:02

标签: jquery forms validation

我已经自定义了我的表单以验证必填字段,但即使所有字段都填写正确,它也不会提交。根据我的理解,jquery.validate.min.js阻止它被提交。有谁知道为什么我的表格没有提交?

代码如下:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
    <script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
    <link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">
</head>


<div>
<form id="myform" method="POST" action="">

<label for="fname">First Name *: </label>
<input id="fname" name="fname">
<br/>
<label for="lname">Last Name *: </label>
<input id="lname" name="lname">
<br/>
<label for="email">Email *: </label>
<input id="email" name="email">
<br/>
<label for="jtitle">Job Title *: </label>
<input id="jtitle" name="jtitle">
<br/>
<label for="orgname">Organization *: </label>
<input id="orgname" name="orgname">
<br/>
<label for="orgname">Describe Yourself *: </label>
<input id="desyourself" name="desyourself" type="hidden" value="123">
<select id="desyourself" name="desyourself">
                    <option value="">
                    Choose One...       </option>
                    <option value="124">
                    Customer
                    </option>
                    <option value="125">
                    Partner
                    </option>
                    <option value="126">
                    Prospect
                    </option>
                    <option value="127">
                    Other
                    </option>
                    <option value="128">
                    Misc.
                    </option>
</select>
<br/>
<input type="submit" value="Validate!">
</form>
</div>



<script>
// avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});

$.validator.methods.email = function( value, element ) {
  return this.optional( element ) || /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/.test( value );
}

$( "#myform" ).validate({
  rules: {
    fname: {
        required: true,
        minlength: 1,
        maxlength: 30,
        lettersonly: true
    },
    lname: {
        required: true,
        minlength: 1,
        maxlength: 30,
        lettersonly: true
    },
    email: {
        required: true,
        email: true
    },
    jtitle: {
        required: true,
        minlength: 1,
        maxlength: 30
    },
    orgname: {
        required: true,
        minlength: 1,
        maxlength: 30
    },
    desyourself: {
        required: true,
    }
  }
});
</script>

2 个答案:

答案 0 :(得分:0)

以下行阻止您的表单提交:

// avoids form submit
jQuery.validator.setDefaults({
     debug: true,
     success: "valid"

});

答案 1 :(得分:0)

  

debug(默认值:false)类型:Boolean启用调试模式。如果是真的,那就是   表单未提交,控制台上显示某些错误   (将检查是否存在window.console属性)。尝试启用时   表单刚刚提交,而不是验证停止提交。

https://jqueryvalidation.org/validate/