使用JQuery验证插件时出错

时间:2013-06-17 19:49:27

标签: jquery jquery-validate

这是我正在使用的代码:

$(document).ready(function() {



    jQuery.validator.addMethod("val_address", function(value, element) {
        return  (value == 'Address only here...');
    }, "Address must be set to a value other than default...");

    jQuery.validator.addMethod("val_city", function(value, element) {
        return (value == 'City only here...');
    }, "City must be set to a value other than default...");

    jQuery.validator.addMethod("val_zip", function(value, element) {
        return (value == 'Zip only here...');
    }, "Zip must be set to a value other than default...");

    jQuery.validator.addMethod("val_state", function(value, element) {
        return (value == 'State only here...');
    }, "State must be set to a value other than default...");

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


    var ruleSetaddress = {
        val_address : true,
        required: true    
    };

    var ruleSetcity = {
        val_city : true,
        required: true
    };

    var ruleSetstate = {
        val_state : true,
        required: true
    };

    var ruleSetzip = {
        val_zip : true,
        required: true
    };

    $("#advanced-searchform").validate({
        rules: {                            
            search_address: ruleSetaddress,
            search_city: ruleSetcity,
            search_state: ruleSetstate,
            search_zip: ruleSetzip     
        },  
        errorElement: "span" ,                              
        messages: {
            search_address: " Enter Address",
            search_city: " Enter City",
            search_state: " Enter State",
            search_zip: " Enter Zip",

        }
    });
    $("#searchform").validate({
        rules: {                            
            search_address: ruleSetaddress,
            search_city: ruleSetcity,
            search_state: ruleSetstate,
            search_zip: ruleSetzip     
        },  
        errorElement: "span" ,                              
        messages: {
            search_address: " Enter Address",
            search_city: " Enter City",
            search_state: " Enter State",
            search_zip: " Enter Zip",
        }
    });

}

您可以在此处查看表单 - > http://dimitri.clientdemos.pw/

由于某种原因,没有任何事情发生 - 使用PHP代码获得的值基本上显示为默认值。所以规则是检查默认值是否保持不变(不应该这样做),并检查是否正在发送空值(同样不应该这样做)。

上述代码有什么问题?

更新我现在可以看到一条警告消息 - 当我发出随机警报消息时 - 但我怎么知道在哪里显示错误消息?我如何定义这样的元素?在我上面的代码中

 errorElement: "span" ,                              

这是什么?它是文本框/ span元素的ID吗?如何定义这样的错误元素?

0 个答案:

没有答案