0

我已经阅读了其他帖子,但无法弄清楚为什么只有我的一些必填字段在提交时未经过验证。我正在帮助客户端验证他的脚本并将表单发布给另一方。在我的服务器上: http: //alluringassets.com/caitlin/projects/validationscript/index.html。如果我关注事件日期,它将验证信息,但如果我从不关注它,然后单击提交 - 程序不会验证位置、组大小、事件日期,但会验证顶部字段。有任何想法吗?

$.validator.addMethod("groupMin", function(value) {
        return $("#00NG0000008iHKH").val() >= 6;
    }, 'Sorry, but we only handle groups of 6 or more.');

$.validator.addMethod("seoNO", function(value) {
        return $("#00NG0000008iHKg").match(/SEO/g);
    }, 'No thank you! We are not interested in Search Engine Optimization support.');

$.validator.setDefaults({
    //submitHandler: function() { 
        //form.submit();
        //alert("submitted!"); }
});

$().ready(function() { 
var validator = $("#detailRequestForm").bind("invalid-form.validate", function() {
            $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors. Please correct.");
        }).validate({
        rules: {
            firstname: "required",
            lastname: "required",
            email: {
                required: true,
                email: true
            },
            company: {
                required: true,
                minlength: 2
            },
            '00NG0000008iHK7': {//Program Location
                required: true,
                minlength: 2
            },
            '00NG0000008iHKH': //Group Size
                "groupMin",
            '00NG0000008iHKR': {//Event Date
                required: true,
                minlength: 3
            },
            '00NG0000008iHKg':"seoNO",
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            email: "Please enter a valid email address",
            company: "We do not serve private groups. Sorry, we can't help you.",
            '00NG0000008iHK7': //Program Location
            "Where will the program be located?",
            '00NG0000008iHKH':{//Group Size
                required:"Please enter the number of members in the group.",
            },
            '00NG0000008iHKR': {//Event Date
                required: "Please enter a date or TBA if you are unsure."
            },
            '00NG0000008iHKg': {//Comments
                //required: "Please enter any comments or questions you may have."
            }
        },
        errorElement: "em",
        errorContainer: $("#warning, #summary"),
        //highlight: function(label) {
            //$(label).removeClass("success").addClass('error');
        //},
        //success: function(label) {
                //label.text("ok!").empty().addClass("success");

            //}

    });
  });
4

2 回答 2

0

不知何故,我想通了。如果有人对以下作品感兴趣。

// JavaScript Document
//Script written by Caitlin Havener
//Utilizing jQuery validation plugin
//Visit sneakymommedia.com to hire me!
$.validator.addMethod("groupMin", function(value) {
        return $("#00NG0000008iHKH").val() >= 6;
    }, 'Sorry, but we only handle groups of 6 or more.');

    /*$.validator.addMethod("seoNO", function(value) {
            return $("#00NG0000008iHKg").match("/SEO/g");
        }, 'No thank you! We are not interested in Search Engine Optimization support.');*/

    $.validator.setDefaults({
        submitHandler: function() { 
            //form.submit();
            alert("submitted!"); }
});

$.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || !re.test(value);
        },
        "No thank you! We are not interested in Search Engine Optimization support."
);


$().ready(function() { 
    var validator = $("#detailRequestForm").bind("invalid-form.validate", function() {
            $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors. Please correct.");
        }).validate({
        //debug: true,
        rules: {
            firstname: "required",
            lastname: "required",
            email: {
                required: true,
                email: true
            },
            company: {
                required: true,
                minlength: 2
            },
            '00NG0000008iHK7': {//Program Location
                required: true,
                minlength: 2
            },
            '00NG0000008iHKH': //Group Size
                "groupMin",
            '00NG0000008iHKR': {//Event Date
                required: true,
                minlength: 3
            },
            '00NG0000008iHKg':{
                //"seoNO",
                regex: / SEO |SEO| seo |seo|Search Engine Optimization | Search Engine Optimization |Search Engine Optimization/
            }
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            email: "Please enter a valid email address",
            company: "We do not serve private groups. Sorry, we can't help you.",
            '00NG0000008iHK7': //Program Location
            "Where will the program be located?",
            '00NG0000008iHKH':{//Group Size
                required:"Please enter the number of members in the group.",
            },
            '00NG0000008iHKR': {//Event Date
                required: "Please enter a date or TBA if you are unsure."
            },
            '00NG0000008iHKg': {//Comments
                //required: "Please enter any comments or questions you may have."
            }
        },
        errorElement: "em",
        errorContainer: $("#warning, #summary"),
        validClass: "success",
        focusCleanup: true,
        success: function(label) {
                label.text("ok!").addClass("success");
            }
    });

});
于 2012-11-29T19:02:19.437 回答
-1

ID 不应该以数字开头——这可能会影响验证插件。是什么阻止您调用这些字段location,groupsizeeventdate

更新

按照声明字段的逻辑,后面的字段缺少一些大括号:

        company: {
            required: true,
            minlength: 2
        },
        '00NG0000008iHK7': { //Program Location = missing opening curly
            required: true,
            minlength: 2
        },
        '00NG0000008iHKH': { //Group Size - opening curly
            "groupMin"}, // closing curly
        '00NG0000008iHKR': { //Event Date - opening curly
            required: true,
            minlength: 3
        },
        '00NG0000008iHKg': {
            "seoNO", // opening curly
        }
于 2012-11-27T01:29:31.937 回答