如何在两个文本框中进行验证 [英] How to give validation in two textbox

查看:107
本文介绍了如何在两个文本框中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本有问题. 我在两个文本框中提供给定验证的功能. 这里有人可以帮助我吗?

I have a problem in this script. My function about given validation in two textbox. anybody here can help me?

我的功能:

<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>

推荐答案

在每个文本框上应用相同的类,并在类上应用setCustomValidity,如下所示,希望对您有所帮助

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" />

这篇关于如何在两个文本框中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆