这是html
<form name="passwordchng" method="post" action="">
<input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
<input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" />
<input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
<input type="submit" name="submit" value="Save" />
</form>
这是jquery
<script>
$("#newpasswd").keyup(function() {
if($("#curpasswd").val()==$("#newpasswd").val()){
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
return false;
}
else{
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character.");
return true;
}
});
</script>
如果条件执行,则不应允许提交表单。这里的表格提交甚至两个密码都相同......
答案 0 :(得分:1)
如果要返回true或false,请在submit
而不是keyup
上使用验证。像:
$("#passwordchng").submit(function() {
if($("#curpasswd").val()==$("#newpasswd").val())
{
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
return false;
}
else
{
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character.");
return true;
}
});
注意: Id 已提供表格
答案 1 :(得分:0)
您可以使用以下条件禁用“if if condition”中的“提交”按钮:
$('input[type="submit"]').attr('disabled','disabled');
并使用以下其他条件再次启用:
$('input[type="submit"]').removeAttr('disabled');
完整代码:
$("#newpasswd").keyup(function() {
if($("#curpasswd").val()==$("#newpasswd").val())
{
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("No changes made to your password.");
$('input[type="submit"]').attr('disabled','disabled');
}
else
{
$(".newpasserror").css({"color":"red","margin-left":"10px"}).text("A strong Password should have at least 1 alphabet,1 digit and 1 special character.");
$('input[type="submit"]').removeAttr('disabled');
}});
答案 2 :(得分:0)
试试这个..
<form name="passwordchng" method="post" action="">
<input name="_curpasswd" id="curpasswd" size="20" autocomplete="off" type="password" />
<input name="_newpasswd" id="newpasswd" size="20" autocomplete="off" type="password" onChange="password();"/>
<input name="_confpasswd" id="confpasswd" size="20" autocomplete="off" type="password" />
<input type="submit" name="submit" value="Save" />
<div id="message"></div>
</form>
<script>
function password() {
var curpasswd = $("#curpasswd").val();
var newpasswd = $("#newpasswd").val();
if (curpasswd != newpasswd)
$("#message").html("Passwords do not match!");
else
$("#message").html("Passwords match.");
}
$(document).ready(function () {
$("#newpasswd").keyup(password);
});
</script>