javascript - jquery validate plugin - validate only if one of the field is not blank - Stack Overflow

I should be able to save the username without changing the password.and validate #frmMyprofile only if c

I should be able to save the username without changing the password.and validate #frmMyprofile only if current password is not blank.How to do that?

Javascript Validation code:

$('#frmMyProfile').validate({
        rules: {
            "user[current_password]": "required",
            "user[password]": {
                required: true,
                minlength: 6
            },
            "user[password_confirmation]": {
                required: true,
                minlength: 6,
                equalTo: "#user_password"
            }
        },
        messages: {
            "user[current_password]": "Please enter your current password",
            "user[password]": {
                required: "Please enter new password",
                minlength: "Please enter at least 6 characters"
            },
            "user[password_confirmation]": {
                required: "Please enter confirm password",
                equalTo: "Please enter the same value as the new password."
            }
        }
});

HTML: I should be able to save only user name or only password or both.It should only validate if current password is not blank.

<form accept-charset="UTF-8" action="/users" class="form-stacked" id="frmMyProfile" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden""></div>          <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_id">Email ID</label>
                      <div class="controls">
                        <input disabled="disabled" type="email" value="[email protected]">
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_name">User Name</label>
                      <div class="controls">
                        <input name="user[name]" type="text" value="test" class="valid">
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_current_password">Current Password</label>
                      <div class="controls">
                        <input id="user_current_password" name="user[current_password]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_password">New Password</label>
                      <div class="controls">
                        <input id="user_password" name="user[password]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_password_confirmation">Confirm Password</label>
                      <div class="controls">
                        <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label></label>
                    </div>
                    <div class="controls">
                      <input name="version" type="hidden" value="v2">
                      <button class="btn btn-primary qbol-button-save" type="submit">Save</button>
                      <button class="btn qbol-button-clear" type="reset">Clear</button>
                    </div>
                  </div>
                </div>
      </form>

I should be able to save the username without changing the password.and validate #frmMyprofile only if current password is not blank.How to do that?

Javascript Validation code:

$('#frmMyProfile').validate({
        rules: {
            "user[current_password]": "required",
            "user[password]": {
                required: true,
                minlength: 6
            },
            "user[password_confirmation]": {
                required: true,
                minlength: 6,
                equalTo: "#user_password"
            }
        },
        messages: {
            "user[current_password]": "Please enter your current password",
            "user[password]": {
                required: "Please enter new password",
                minlength: "Please enter at least 6 characters"
            },
            "user[password_confirmation]": {
                required: "Please enter confirm password",
                equalTo: "Please enter the same value as the new password."
            }
        }
});

HTML: I should be able to save only user name or only password or both.It should only validate if current password is not blank.

<form accept-charset="UTF-8" action="/users" class="form-stacked" id="frmMyProfile" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden""></div>          <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_id">Email ID</label>
                      <div class="controls">
                        <input disabled="disabled" type="email" value="[email protected]">
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_name">User Name</label>
                      <div class="controls">
                        <input name="user[name]" type="text" value="test" class="valid">
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-horizontal">
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_current_password">Current Password</label>
                      <div class="controls">
                        <input id="user_current_password" name="user[current_password]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_password">New Password</label>
                      <div class="controls">
                        <input id="user_password" name="user[password]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label class="control-label" for="user_password_confirmation">Confirm Password</label>
                      <div class="controls">
                        <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password">
                      </div>
                    </div>
                  </div>
                  <div class="form-row control-group">
                    <div class="input">
                      <label></label>
                    </div>
                    <div class="controls">
                      <input name="version" type="hidden" value="v2">
                      <button class="btn btn-primary qbol-button-save" type="submit">Save</button>
                      <button class="btn qbol-button-clear" type="reset">Clear</button>
                    </div>
                  </div>
                </div>
      </form>
Share Improve this question edited Aug 20, 2014 at 8:57 Tabraiz Ali asked Aug 20, 2014 at 8:27 Tabraiz AliTabraiz Ali 6779 silver badges36 bronze badges 1
  • share your html also – Arun P Johny Commented Aug 20, 2014 at 8:30
Add a ment  | 

1 Answer 1

Reset to default 7

Try

function passwordRequired() {
    return $('#user_current_password').val().length > 0;
}
$('#frmMyProfile').validate({
    rules: {
        "user[current_password]": {
            required: function () {
                return $('#user_password').val().length > 0 || $('#user_password_confirmation').val().length > 0;;
            }
        },
        "user[password]": {
            required: passwordRequired,
            minlength: {
                param: 6,
                depends: passwordRequired
            }
        },
        "user[password_confirmation]": {
            required: passwordRequired,
            minlength: {
                param: 6,
                depends: passwordRequired
            },
            equalTo: {
                param: "#user_password",
                depends: passwordRequired
            }
        }
    },
    messages: {
        "user[current_password]": "Please enter your current password",
        "user[password]": {
            required: "Please enter new password",
            minlength: "Please enter at least 6 characters"
        },
        "user[password_confirmation]": {
            required: "Please enter confirm password",
            equalTo: "Please enter the same value as the new password."
        }
    }
});

Demo: Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744268238a4565970.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信