6

我已经为此工作了几个小时,但似乎无法使其工作。在我们的客户部分,我需要不止一封电子邮件,在同一个输入字段中。

现在我有(这对一封电子邮件很有用):

<script language="JavaScript">
$(document).ready(function(){
    $("form#editclient").validate({
            rules: {
            Contact: {
                required: true
            },
            Clientname: {
                required: true
            },
            ClientLogin: {
                required: true
            },
            ClientPassword: {
                required: true
            },
            email: {
                required: true,
                multiemail: true
            }
        },
        messages: {
            Contact: {
                required: "Please enter your Contact Nmae."
            },
            Clientname: {
                required: "Please enter your Client Name"
            },
            ClientLogin: {
                required: "Please enter a login."
            },
            ClientPassword: {
                required: "Please enter a password"
            },
            email: {
                required: "Please enter an Email Address",
                multiemail: "You must enter a valid email, or comma separate multiple"
            }
        }
});
});
</script>
    <input type="Text" id="email" name="Email" value="#Trim(Email)#" maxlength="60" class="inputText430 email required">

我发现这应该让多个验证器工作:

jQuery.validator.addMethod(
"multiemails",

function (value, element) {
    if (this.optional(element)) // return true on optional element
    return true;
    var email = value.split(/[;,]+/); // split element by , and ;
    valid = true;
    for (var i in email) {
        value = email[i];
        valid = valid && jQuery.validator.methods.email.call(this, $.trim(value), element);
    }
    return valid;
},

jQuery.validator.messages.multiemail);

但我无法让它正常工作。有人可以帮我在一个文本字段中验证 2 封或更多封电子邮件吗?

提前致谢。

(编辑以在规则和消息中添加多电子邮件)(编辑以下解决方案)解决方案

<form>
    <input id='emailTest' name='emailTest' />
    <input type='submit' />
</form>
    <script type="text/javascript">
    jQuery.validator.addMethod(
    "multiemail",
    function (value, element) {
        var email = value.split(/[;,]+/); // split element by , and ;
        valid = true;
        for (var i in email) {
            value = email[i];
            valid = valid && jQuery.validator.methods.email.call(this, $.trim(value), element);
        }
        return valid;
    },
    jQuery.validator.messages.multiemail
);

$("form").validate({
    debug: true,
    rules: {
            emailTest: {
                multiemail: true
            }
        },
    messages: {
            emailTest: {
                multiemail: "You must enter a valid email, or comma separate multiple"
            }
        },
    submitHandler: function(form) {
            return false;
        }
});
4

1 回答 1

12

I think there are some semantic errors in the code which lead to some things happening you aren't expecting.

I have managed to get your custom handler to work by mapping it to a field by name rather than by type - I'm not sure the override works in the plugin.

rules: {
    emailTest: {
        multiemail: true
    }
}

Also in the handler, you had multiemails as the identifier and referred to it as multiemail later on, not sure how much of an impact that had.

Here's the full working example: http://jsfiddle.net/jbergler/7jXn7/2/

于 2013-07-29T23:02:45.170 回答