How to give validation in two textbox

落花浮王杯 提交于 2019-12-12 00:46:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!