0

I want to validate(not allow blank) any field out of these two !Got some idea from here but unable to implement it in 1.9.1 version.

Even both of the fields are validate,that's also fine. Any inputs..... guys

Here is my 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"] }
    }
});

​and here is my 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>

I want just to validate any field out of these two blank fields.

Community
  • 1
  • 1
bigZero
  • 121
  • 1
  • 1
  • 5

1 Answers1

0

Add a minlength property to each of the inputs. If you add minlength 1 they can't be blank. If you add the class "email" it will validate an email address.

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');
        }
    });
});

Ex: http://jsfiddle.net/jeffshaver/K2XAX/3/

Jeff Shaver
  • 3,315
  • 18
  • 19
  • Jeff thanks for this response,but want to validate blank any of the field before submit. but it's showing #403 & CSRF verification failed. Request aborted. – bigZero Feb 28 '13 at 12:26
  • http://jsfiddle.net/jeffshaver/K2XAX/2/ This is working fine. It validates before submitting. Just disable the submit button until one is filled out – Jeff Shaver Feb 28 '13 at 12:46