HTML表单客户端验证
HTML FORM CLIENT VALIDATION
我是javascript场景的新手,更不用说在rails应用程序上使用它了。所以我决定做一个客户端验证我的注册形式一切工作正常,但我的脚本检查密码是否匹配确认密码。每次它告诉我密码不匹配,我真的希望有人能帮我这个。提前感谢:)P.S这主要是html和javascript
<head>
<form id="sign_up" method="post" action="/auth/identity/register">
<div class="field">
<input id="name" class="username" type="text" placeholder="Full name" name="name" required></input>
</div>
<div class="field">
<input id="email" class="username" type="email" placeholder="Email address" name="email" required></input>
</div>
<div class="field">
<input id="password" class="username" type="password" placeholder="Password" name="password" required></input>
</div>
<div class="field">
<input id="password_confirmation" class="username" type="password" placeholder="Password confirmation" name="password_confirmation" required></input>
</div>
<div class="field">
<input class="btn btn-primary" type="submit" value="Sign up" name="commit"></input>
</div>
</form>
<script type="text/javascript">
window.onload = function () {
document.getElementById("password").onchange = validatePassword;
document.getElementById("password_confirmation").onchange = validatePassword;
}
function validatePassword(){
var pass2=document.getElementById("password_confirmation").value;
var pass1=document.getElementById("password").value;
if(pass1!=pass2){
document.getElementById("password_confirmation").setCustomValidity("Passwords Don't Match");
}
else
document.getElementById("password_confirmation").setCustomValidity('');
//empty string means no validation error
}
</script>
</div>
</head>
正如评论者所说,当您将代码移动到文档的<body>
中时,它就可以工作了。也许在文档中有多个ID为password
或password_confirmation
的元素?
console.log("Password box value: "+pass1);
console.log("Password confirmation box value: "+pass2);
把这些行放在var pass1=document.getElementById("password").value;
然后您可以在浏览器控制台(F12
键)中直观地检查输入的值,以确保它们是相同的。在单击提交按钮后检查这些值。请记住,您将onchange
事件绑定到输入,因此当您从密码输入移动到密码确认输入时,您将看到日志记录(您可以忽略这一点,因为您还没有输入确认密码)。
最后,值得注意的是,Firefox在用户反馈方面与Chrome略有不同;当你输入错误的文本时,Firefox会在文本周围显示红光,当在密码确认输入中输入的密码相同时,红光就会消失。Chrome不会这样做,只会在你点击提交按钮后提示密码不匹配。
相关文章:
- 客户端单选按钮列表验证
- 如何在不使用mvc 4中的模型的情况下进行客户端验证
- ServiceStack JavaScript服务器事件客户端的身份验证
- MVC客户端验证的某个位置缺少链接
- 从javascript客户端验证REST服务
- asp.net web表单客户端验证始终回发
- 如何在文本框中验证客户端的youtube url
- 如何验证客户端上的Javascript没有被更改
- 检查验证组是否有效,而不启动验证客户端
- Ember用户身份验证(客户端和服务器端完全分离)
- 是否不可能通过/Regex_syntax/.test(str_to_test)验证客户端输入,其中Regex_synta
- 调试不显眼的验证(客户端)
- Asp.net MVC日期时间动态验证客户端日期格式
- 尝试访问HTML5电子邮件验证客户端
- 验证客户端日期范围
- 表单验证客户端和服务器端
- 如何验证客户端中文本框值的总和
- 验证客户端AngularJS上输入的图像文件维度
- JSON Web令牌(JWT)-验证客户端
- MVC 5 - 验证客户端上的特定字段