Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  188] [ 4]  / answers: 1 / hits: 5809  / 10 Years ago, wed, august 20, 2014, 12:00:00

jquery



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 [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>

More From » jquery

 Answers
1

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


[#43021] Monday, August 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brynn

Total Points: 448
Total Questions: 83
Total Answers: 118

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
;