我对javascript场景还挺陌生,更不用说在rails应用程序上使用它了。所以我决定做一个客户端验证我的注册表单一切正常,但我的脚本检查密码是否匹配确认密码。每次它告诉我密码不匹配我真的希望有人能帮助我。感谢提前:) P.S这主要是HTML和JavaScriptHTML FORM客户端验证
<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>
你能显示你的完整的html和js代码吗? –
这是我的整个JS代码,我做了一切在HTML – calixProg
移动你的形式和脚本正文。为我工作得很好。 (在firefox24和chrome33上测试) – anu