【问题标题】:jquery validate with multiple rulesjquery 使用多个规则进行验证
【发布时间】:2010-10-08 12:22:38
【问题描述】:

如何使用 div 容器中的三个自定义消息验证具有三个规则的电子邮件地址字段。 IE。

rules: {
    email: {
        validationRule: true,
        email: true,
        remote: '/ajax/emailDuplicationCheck.php'
       }
     }

如果第一个错误消息应该是“验证规则失败”

如果第二个错误(失败)“输入电子邮件地址”

如果第三个(远程)失败。消息应为“帐户已存在于数据库中”。

我可以为所有规则添加一条消息,但我想根据规则自定义消息。

【问题讨论】:

    标签: jquery validation jquery-validate


    【解决方案1】:

    试试这个:

    $("#myForm").validate({ // Replace #myForm with the ID of your form
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    url: "/ajax/emailDuplicationCheck.php",
                    type: "post",
                    data: { email: function() {
                        return $("#email").val(); // Add #email ID to your email field
                    }
                }
            }
        },
        messages: {
            email: {
                required: 'Email address is required',
                email: 'Please enter a valid email address',
                remote: 'This email address has already been used'
            }
        },
        errorPlacement: function(error) {
            $("#response").html(error);
        }
    });
    

    希望这会有所帮助!

    【讨论】:

    • 没有。它不工作。它仅在电子邮件中显示消息:“输入电子邮件地址”。如我所说。我想在 div 容器中显示 msg?
    • 语法是正确的,应该是这样,所以问题一定出在其他地方。 validationRule 看起来像自定义验证方法。它到底应该做什么?您能否编辑您的问题并添加该方法的代码?
    • validationRule 方法什么都不做。它只是返回true。好吧,如果我删除那条线。即使它不符合两条规则。以及如何在 id #response 的 Divcontainer 中显示它。我想在 div 容器中显示消息
    • 请查看更新后的答案。另请注意,为了使远程验证正常工作,您的emailDuplicationCheck.php 文件应侦听$_POST['email'],如果使用电子邮件,则仅输出false,如果未使用电子邮件,则仅输出true
    • @ToniMichelCaubet 正如我在上面的第二条评论中所说,如果电子邮件未使用,则应返回 true,如果电子邮件已在使用,则应返回 false
    【解决方案2】:

    您可以为每个规则使用自定义验证规则以及您自己的自定义消息。

    为简单起见,下面是一个示例,说明如何使用三种自定义验证方法验证“用户名”输入(每种方法都有自己的错误消息):

    // Add three custom methods first:
    
    $.validator.addMethod("nameCustom", function(value, element) {
        return this.optional(element) || /^[a-zA-Z ]+/.test(value);
    }, 'Please use english letters only.');
    
    $.validator.addMethod("noSpaceStart", function(value, element) { 
        return value.indexOf(" ") != 0; 
    }, "Name cannot start with a space");
    
    $.validator.addMethod("noSpaceEnd", function(value, element) { 
        return value.lastIndexOf(" ") != value.length - 1; 
    }, "Name cannot end with a space");
    
    $('#form_to_validate').validate({
        rules: {
            username: {
                // Add the custom validation methods to the username input
                noSpaceStart: true,
                noSpaceEnd: true,
                nameCustom: true,
                // required and maxlength are built-in methods in the plugin and are ready to be used
                required: true,
                maxlength: 50
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      相关资源
      最近更新 更多