JQuery表单验证禁用整个JQuery代码

时间:2014-12-17 05:40:26

标签: javascript jquery jquery-validate

J Query表单验证禁用整个JQuery Code.I使用J Query创建了一个简单的验证表单。它不起作用。 我已经尝试了http://jsfiddle.net/4PuJL/165/并且工作正常,但没有在我的电脑上工作。

<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
<script src="http://malsup.github.io/jquery.form.js"></script>
<script src="http://http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<script type="text/javascript">         
    $(document).ready(function(){
     $("#p1").mousedown(function () {
            alert("Mouse down over p1!");
        });       
        $("#personDetailForm").validate({
            rules: {
                persname: {
                    required: true,
                    regex: /^[A-Za-z]+$/
                }
            }
            submitHandler: function() {                        
                alert("alert");
            }
        });        
    });
    </script>
   </head>
 <body>        
  <p id="p1">This is a paragraph.</p>
  <form id="personDetailForm" action="showDetails.html" method="post"  novalidate="novalidate" >
  <p>
    Name:
  <input type="text" name="persname" id="persname" maxlength="50"></p><br>
  Password:
  <input type="password" name="perspswd" id="perspswd"><br>
  <input type="reset" value="Reset">
  <button type="submit">Submit</button>  
  </form>
  <span></span>
 </body>
</html>

如果我删除下面的部分,则第一个警报开始工作

$("#personDetailForm").validate({
  rules: {
  persname: {
  required: true,
  regex: /^[A-Za-z]+$/
  }
 }
 submitHandler: function() {                        
 alert("alert");
 }
});   

3 个答案:

答案 0 :(得分:1)

$("#personDetailForm").validate({
   rules: {
      persname: {
         required: true,
         regex: /^[A-Za-z]+$/
      }
   }
   submitHandler: function() {                        
      alert("alert");
   }
});  

,rules之间应为submitHandler

你的小提琴中没有submitHandler部分,这就是它工作的原因。

答案 1 :(得分:0)

<head>部分中存在错误。

你包括这个:

<script src="http://http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>

哪个错了。 试试这个:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>

答案 2 :(得分:0)

实际上有三个问题......

  1. 此插件的脚本包含错误地http://写了两次。

    src="http://http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"
    
  2. 没有名为regex的规则。如果您添加the additional-methods.js file,那么您可以使用pattern方法。

  3. 您在rules选项...

    后缺少逗号
    rules: {
        persname: {
            required: true,
            pattern: /^[A-Za-z]+$/  // <- use 'pattern' in place of "regex"
        }
    }, // <- need a comma here
    submitHandler: function() {                        
        alert("alert");
    }
    
  4. DEMO:http://jsfiddle.net/c6L3Loot/