JQuery 验证器更新密码
我有一个包含三个字段的表单
- 旧密码
- 新密码
- 确认密码
这些字段是可选的,仅当用户在新密码中输入一些文本时才必须输入 我如何应用 jQuery Validator 插件来实现同样的功能。
jQuery.validator.addMethod("pass2",
function(value,element,param) {
var op=document.forms[0].oldpass;
var newp=document.forms[0].newpass1;
var cp=document.forms[0].newpass2;
if(newp.value != "" )
{ if(element.name=="newpass2")
{ if(cp.value!=newp.value) {return true;}
else {return false;}
}
if(element.name=="oldpass")
{ if(op.value=="") {return true;}
else { return false;}
}
}
else
return true;
},"");
$(document).ready(function() {
$("#form1").validate({
debug:true,
rules: {
oldpass:
{
pass2:true
},
/*newpass1:
{
pass1:true
},*/
newpass2: {
pass2:true
}
},
messages: {
oldpass: "Please enter a oldpass",
//newpass1: "Please enter newpassword",
newpass2: "Please confirm password"
}
});
});
">I have a form which have three fields
- Old Password
- New Password
- Confirm Password
These fields are optional and only mandatory when user enter some text into New Password
How can i apply jQuery Validator plugin for the same.
jQuery.validator.addMethod("pass2",
function(value,element,param) {
var op=document.forms[0].oldpass;
var newp=document.forms[0].newpass1;
var cp=document.forms[0].newpass2;
if(newp.value != "" )
{ if(element.name=="newpass2")
{ if(cp.value!=newp.value) {return true;}
else {return false;}
}
if(element.name=="oldpass")
{ if(op.value=="") {return true;}
else { return false;}
}
}
else
return true;
},"");
$(document).ready(function() {
$("#form1").validate({
debug:true,
rules: {
oldpass:
{
pass2:true
},
/*newpass1:
{
pass1:true
},*/
newpass2: {
pass2:true
}
},
messages: {
oldpass: "Please enter a oldpass",
//newpass1: "Please enter newpassword",
newpass2: "Please confirm password"
}
});
});
">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为此添加您自己的验证方法。
这允许您指定一个 JavaScript 函数来确定给定的输入字段是否有效。在这种情况下,如果 NewPassword 非空且给定字段为空,该函数将返回 false。将该方法附加到您的 OldPassword 和 ConfirmPassword 字段,然后您就完成了。
You need to add your own validation method for that.
This allows you to specify a javascript function that determines if a given input field is valid or not. In this case, the function would return false if the NewPassword is non-empty and the given field is empty. Attach that method to your OldPassword and ConfirmPassword fields and you are done.