密码确认验证未按预期显示
Password confirmation validation not showing as intended
这是我今天早些时候提出的问题的扩展(它仍未解决,非常感谢任何帮助)。我已经看到了很多关于堆栈溢出的问题,我的代码是基于我在教程网站上找到的东西。
我有 2 个密码表单,我想验证。如果他们匹配,我只想提交它。这是我的代码-
<li>
<div class="input-group col-xs-5 pw">
<input type="password" name="pw" class="form-control" placeholder="Enter a new password" id="new-pw" />
</div>
</li>
<li>
<div class="input-group col-xs-5">
<input type="password" name="cnfrm-pw" id="cnfrm-pw" class="form-control" placeholder="Confirm new password" />
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" id="pw-btn"> <em class="glyphicon glyphicon-circle-arrow-right"> </em> </button>
</span>
</div>
</li>
我的脚本-
$(document).ready(function () {
$('#pw-btn').click( function() {
var pwd1 = document.getElementById("new-pw").value;
var pwd2 = document.getElementById("cnfrm-pw").value;
if (pwd1 != pwd2)
{
document.getElementById("cnfrm-pw").setCustomValidity("Passwords Don't Match");
}
else {
document.getElementById("cnfrm-pw").setCustomValidity('');
//empty string means no validation error
}
}
);
});
我期待一个HTML5验证表单,它告诉我密码不匹配,就像这样。然而,什么也没发生。我的自定义验证方法是否存在错误?提前谢谢大家,非常感谢大家的帮助和建议。
答
我使用以下代码来使其工作。这是对提交的答复的修改。感谢所有的帮助!
.HTML:
<form action="#" id="f" method="post">
<li>
<div class="input-group col-xs-5 pw">
<input type="password" name="pw" class="form-control new-pw" placeholder="Enter a new password" id="new-pw" required pattern="(?=.*'d)(.{6,})" />
</div>
</li>
<li>
<div class="input-group col-xs-5">
<input type="password" name="cnfrm-pw" id="cnfrm-pw" class="form-control cnfrm-pw" required placeholder="Confirm new password" />
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" id="pw-btn"> </button>
</span>
</div>
</li>
</form>
.JS
$(document).ready(function () { //This confirms if the 2 passwords match
$('#f').on('keyup', '.cnfrm-pw', function (e) {
var pwd1 = document.getElementById("new-pw").value;
var pwd2 = document.getElementById("cnfrm-pw").value;
if (pwd1 != pwd2) {
document.getElementById("cnfrm-pw").setCustomValidity("The passwords don't match"); //The document.getElementById("cnfrm-pw") selects the id, not the value
}
else {
document.getElementById("cnfrm-pw").setCustomValidity("");
//empty string means no validation error
}
e.preventDefault(); //would still work if this wasn't present
}
);
});
.setCustomValidy工具提示仅在表单提交时触发。
爪哇语
$(document).ready(function() {
$('#f').submit(function(e) {
var pwd1 = document.getElementById("new-pw").value;
var pwd2 = document.getElementById("cnfrm-pw").value;
if (pwd1 != pwd2) {
document.getElementById("cnfrm-pw").setCustomValidity("Passwords Don't Match");
} else {
document.getElementById("cnfrm-pw").setCustomValidity('');
//empty string means no validation error
}
e.preventDefault();
}
);
});
.HTML
<form action="#" id="f"><li>
<div class="input-group col-xs-5 pw">
<input type="password" name="pw" class="form-control" placeholder="Enter a new password" id="new-pw" />
</div>
</li>
<li>
<div class="input-group col-xs-5">
<input type="password" name="cnfrm-pw" id="cnfrm-pw" class="form-control" placeholder="Confirm new password" />
<span class="input-group-btn">
<input type="submit" />
</span>
</div>
</li>
</form>
看一看:http://codepen.io/anon/pen/WwQBZy
我相信
你缺少required
属性:
<input type="password" name="pw" class="form-control" placeholder="Enter a new password" id="new-pw" required />
请注意末尾的required
。
此外,如果要设置最小值:
<input type="password" name="pw" class="form-control" placeholder="Enter a new password" id="new-pw" pattern=".{5,}" title="Minmimum 5 letters or numbers." required />
至于提交表单,当验证通过时,您可以使用 Web API 中提供的submit()
函数提交表单。你可以在这里阅读它。
相关文章:
- 事件侦听器更新就绪在加载时未显示确认,而更新实际存在
- 无法删除并显示确认框
- 使用模态弹出联系表单,是否可以在提交时在同一弹出窗口中显示确认
- 如何使用 javascript 一键显示确认警报
- 显示 确认删除 从选择框中删除框
- 未显示确认对话框
- Javascript确认框,是/否,它显示确认框,但从不等待使用点击是.它会自动提交表单
- 如何在单击 链接时显示确认对话框
- 在代码点火器中使用 AJAX 单击按钮时,成功更新后显示确认
- 在HTML输入字段中选择文件时显示确认
- 同时显示确认和重定向
- 锂:单击链接时显示确认框
- 有人能帮我在EventHandler上做错了什么吗?出于某种原因,我可以't打开显示确认框
- yii在提交按钮中显示确认消息两次
- 显示确认框时禁用按键
- SAPUI5在退出android中的应用程序之前显示确认
- 当用户尝试刷新/关闭/导航到另一个网页时,显示确认对话框
- 如何显示确认弹出从下拉菜单中选择一个选项
- 在按钮上显示确认对话框,单击使用rails 4
- 如何在倒计时计时器刷新时显示确认框