0

我可以提交我的表单,无论它是空白还是部分填写,这不应该发生,因为我使用的是 jquery 验证器插件。我将插件脚本放在正确的位置,并且验证方法正确附加到右侧表单选择器。还有什么问题?

$("form").submit(function(){
     (".selector").validate({
         rules: {
             performance : {
                  required: true,
                  customvalidation: true
             },
             location : {
                  required: true,
                  customvalidation: true
             },
             date : {
                  required: true,
                  customvalidation: false
             },
             time : {
                  required: true,
                  customvalidation: true
             },
             guests : {
                  required: true,
                  customvalidation: true
             },
             price : {
                  required: true,
                  customvalidation: true
             }
         },
         messages: {
             performance : {
                  required: "enter a show name"
             },
         location : {
             required: "enter a location"
          },
          date : {
             required: "pick a date"
                 },
          time : {
             required: "give a time"
                 },
          guests : {
             required: "how many people do you need?"
          },
          price : {
             required: "what is the cover price?"
          }
     },
     submitHandler: function(form) {
          console.log("now validated");
     }
}); //closes validate        

$.validator.addMethod("customvalidation",
           function(value, element) {
                   return (/^[A-Za-z\d=#$%@_ \-]+$/.test(value));
           },
   "Sorry, no special characters allowed"
   );


var date =  $("#date").val();
var showName = $("#performance").val(); 
var venue = $("#location").val();  
var time =  $("#time").val();
var guests = $("#guests").val();
var price = $("#price").val(); 

//this is where I submit to my database//

});//closes submit
4

1 回答 1

1

修改您的submit函数以event.preventDefault()在验证失败时运行。

通常您可以使用 return false,但由于您使用 jQuery 事件添加了此事件,因此您必须使用stackoverflow 中讨论的event.preventDefault()替代方法。

于 2013-09-23T01:11:15.863 回答