问题
I have a problem in this script. My function about given validation in two textbox. anybody here can help me?
My function:
<script>
window.onload = function () {
document.getElementById("pass").onchange = validatePassword;
document.getElementById("rp_pass").onchange = validatePassword;
};
function validatePassword()
{
var pass2=document.getElementById("pass").value;
var pass1=document.getElementById("rp_pass").value;
if(pass1!=pass2)
document.getElementById("rp_pass").setCustomValidity("Passwords Don't Match");
else
document.getElementById("rp_pass").setCustomValidity('');
//empty string means no validation error
}
</script>
回答1:
Apply same class on each textbox and apply setCustomValidity on class as below hope it helps
window.onload = function () {
document.getElementById("pass").onchange = validatePassword;
document.getElementById("rp_pass").onchange = validatePassword;
};
function validatePassword()
{
var pass2=document.getElementById("pass").value;
var pass1=document.getElementById("rp_pass").value;
if(pass1!=pass2)
document.getElementsByClassName("customvalidity").setCustomValidity("Passwords Don't Match");
else
document.getElementsByClassName("customvalidity").setCustomValidity('');
//empty string means no validation error
}
<input type="text" value="" id="pass" class="customvalidity"/>
<input type="text" value="" id="rp_pass" class="customvalidity" />
来源:https://stackoverflow.com/questions/41330114/how-to-give-validation-in-two-textbox