PHP表单验证不起作用 - 在元素旁边需要错误结果

时间:2016-10-31 17:16:25

标签: php jquery ajax forms validation

PHP新手在这里。

我已经搜索并尝试了各种各样的东西,但仍然无法让它工作......基本上我有一个冗长的表单,要求用户完成几乎所有字段才能正确提交,但我希望表单能够列出元素旁边的错误。

我已经使用了这个code,所以我想要一些类似于它显示错误的内容,然后如果一切都好,它会转移到一个"自定义的感谢页面"。

这是我的代码:

(function() {

    $('form').ajaxForm({
        beforeSubmit: function() {
            //return $('#mailForm').validate().form();
            $("#mailForm").validate({
                 rules: {
                    name: {
                       required: true,
                       minlength : 2
                    },
                    name2: {
                       required: true,
                       minlength : 2
                    },
                    ic-passport: {
                       required: true,
                       minlength : 6
                    },
                    phone: { 
                      required : true,
                      number:true,  
                      minlength : 10,
                      maxlength : 13
                    },
                    city: {
                       required: true,
                       minlength : 4
                    },
                    email:{ 
                       required: true,
                       email: true
                    },
                    dob: {
                       required: true,
                       date: true
                       minlength : 4
                    },
                    sex: {
                       required: true,
                    },
                    tpt: {
                       required: true,
                    },
                    licence: {
                       required: true,
                    },
                    travel: {
                       required: true,
                    },
                    language: {
                       required: true,
                    },
                    ethnicity: {
                       required: true,
                    },
                    religion: {
                       required: true,
                    },
                    age: {
                       required: true,
                    },
                    hair-colour: {
                       required: true,
                    },
                    hair-length: {
                       required: true,
                    },
                    eye-colour: {
                       required: true,
                    },
                    height: {
                       required: true,
                    },
                    weight: {
                       required: true,
                    },
                    image1: "required",
                    bio: "required",
                    experience: "required",
                    femail:{ 
                       required: true,
                       email: true
                    }
                 },
                 messages: {
                        name: {
                           required:"Please enter your name",
                           minlength: "Please enter a valid name"
                        },
                        name2: {
                           required:"Please enter your name",
                           minlength: "Please enter a valid name"
                        },
                        ic-passport: {
                           required:"Please enter your IC/Passport Number",
                           minlength: "Please enter a valid IC/Passport Number"
                        },
                        phone: { 
                           required: "Please enter your phone number",
                           minlength: "Please enter your valid phone number",
                           maxlength: "Please enter your valid phone number"
                        },
                        city: { 
                           required: "Please enter your City/Town & State",
                           minlength: "Please enter your valid City/Town & State",
                        },
                        email:{ 
                           required: "Please enter your email",
                           minlength: "Please enter a valid email address",
                        },
                        dob:{ 
                           required: "Please enter your Date of Birth",
                           minlength: "Please enter a valid Date of Birth",
                        },
                        sex:{ 
                           required: "Please select your Gender",
                        },
                        tpt:{ 
                           required: "Please select if you own Transport",
                        },
                        licence:{ 
                           required: "Please select if you own a Licence",
                        },
                        travel:{ 
                           required: "Please select if you are willing to Travel",
                        },
                        language:{ 
                           required: "Please select what Language you speak",
                        },
                        ethnicity:{ 
                           required: "Please enter your Ethnicity",
                        },
                        religion:{ 
                           required: "Please enter your Religion",
                        },
                        age:{ 
                           required: "Please enter your Age",
                        },
                        hair-colour:{ 
                           required: "Please enter your Hair Colour",
                        },
                        hair-length:{ 
                           required: "Please select your Hair Length",
                        },
                        eye-colour:{ 
                           required: "Please enter your Eye Colour",
                        },
                        height:{ 
                           required: "Please enter your Height",
                        },
                        weight:{ 
                           required: "Please enter your Weight",
                        },
                        image1: "Please Choose at least 1 Photo",
                        bio: "Please enter your Bio",
                        experience: "Please enter your Experience",
                        femail: { 
                           required: "Please enter your email",
                           minlength: "Please enter a valid email address",
                        }
                 },
                 debug: true,
                 errorElement: "em",
                 errorContainer: $("#warning, #summary"),
                 errorPlacement: function(error, element) {
                    error.appendTo( element.parent("td").next("td") );
                 },
                 success: function(label) {
                    label.text("ok!").addClass("success");
                 }  
             });
             var flag= $('#mailForm').valid();
             if(!flag){
                 return false;
             }
        },
        success: function(msg) {
           alert(msg);
           //$("#mailForm").addClass("hide");
           //$("#message1").show();
        },
        complete: function(xhr) {
            $("#mailForm").addClass("hide");
            $("#message1").show();
            $('#status').html(xhr.responseText);    
        }
    }); 
    $('#close').click(function(){ 
      $('#message1').hide();
      $("#mailForm").removeClass("hide");
      $('input[type=text],input[type=email],textarea,input[type=tel],input[type=date],input[type=url],input[type=file]').val('');
      location.reload();
     });
    })();       

0 个答案:

没有答案