0

我想验证(不允许空白)这两个字段中的任何字段!从这里得到一些想法但无法在 1.9.1 版本中实现它。

即使这两个字段都是验证的,那也很好。任何输入.....伙计们

这是我的jQuery

    jQuery.validator.addMethod("require_from_group", function(value, element, options) {
    alert("xxx");
    var valid = $(options[1], element.form).filter(function() {
        return $(this).val();
    }).length >= options[0];

    if(!$(element).data('reval')) {
        var fields = $(options[1], element.form);
        fields.data('reval', true).valid();
        fields.data('reval', false);
    }
    return valid;
}, jQuery.format("'Please enter either username/ email address to recover password'/Please fill out at least {0} of these fields."));

$("#forgot_pass_form").validate({
    rules: {
        uname: { require_from_group: [1,".send_username"] },
        email: { require_from_group: [1,".send_password"] }
    }
});

这是我的html

<form action="#" class="send_form" id="forgot_pass_form" method="POST">
                <fieldset>
                    <div class="send_row">
                        <label class="padding-top10">Email</label>
                        <input type="text" class="send_email" id="email" name="email" />
                        <em>You need to type an email address</em>
                    </div>

                    <div class="send_row option">OR</div>

                    <div class="send_row">
                        <label class="padding-top10">Username</label>
                        <input type="text" class="send_username" id="uname" name="uname" />
                    </div>


                    <div class="send_row send_submitforgotuser">
                        <input type="submit" value="Submit" />
                    </div>
                </fieldset>
                </form>

我只想验证这两个空白字段中的任何字段。

4

1 回答 1

0

为每个输入添加一个 minlength 属性。如果添加 minlength 1 它们不能为空。如果您添加“电子邮件”类,它将验证电子邮件地址。

HTML

<form action="#" class="send_form" id="forgot_pass_form" method="POST">
    <fieldset>
        <div class="send_row">
            <label class="padding-top10">Email</label>
            <input type="text" class="send_email email" id="email" name="email" />
            <em>You need to type an email address</em>
        </div>

        <div class="send_row option">OR</div>

        <div class="send_row">
            <label class="padding-top10">Username</label>
            <input type="text" class="send_username" id="uname" name="uname" minlength="2" />
        </div>


        <div class="send_row send_submitforgotuser">
            <input type="submit" value="Submit" disabled="" />
        </div>
    </fieldset>
</form>

JS:

$(document).ready(function() {
    $("#forgot_pass_form").validate();
    $("#forgot_pass_form input").on('change', function() {
        if ($('#forgot_pass_form').valid()) {
            $('#forgot_pass_form input[type="submit"]').removeAttr('disabled');
        } else {
            $('#forgot_pass_form input[type="submit"]').attr('disabled', 'true');
        }
    });
});

例如:http: //jsfiddle.net/jeffshaver/K2XAX/3/

于 2013-02-28T12:12:44.153 回答