简体   繁体   English

为什么我的密码验证码不起作用?

[英]why my password validation code is not working?

i've made a trail login page but when i'm trying to validate the password...nothing is showing up.. Heres my code 我已经创建了一个跟踪登录页面,但是当我试图验证密码时......没有任何东西出现..继承我的代码

<script type="text/javascript">
    function cmdvalidate()
    {  
        if(form1.pass.value==form1.c_pass.value) {
            if(form1.pass.length<=6) {
                alert("password length must be greater than 6!!");

                form1.pass.value="";
                form1.c_pass.value="";                   
            } else {
                document.write("<font color="red" size=4><br>*password matched successfully<br></font>");
                document.write("Submit you form<br>");
            }
        } else {
            alert("Password not matched!!");

            form1.pass.value="";
            form1.c_pass.value="";
        }         
    }
</script>

Here is a little snippet demonstrating an alternative to your approach. 这是一个小片段,展示了您的方法的替代方案。 You should not use the old form of addressing form elements through formname.elementId but should use the new document.querySelector() or one of the document.getElementById() or document.getElementsBy...() methods instead. 您不应该通过formname.elementId使用旧形式的表单元素,而应使用新的document.querySelector()document.getElementById()document.getElementsBy...()方法之一。

Also note the way I bound the event listener to the <body> element and deferred its action to INPUT:password elements only. 还要注意我将事件监听器绑定到<body>元素并将其操作延迟到INPUT:password元素的方式。

 const qs=s=>document.querySelector(s); // define a little shortcut here ... qs('body').addEventListener('keyup',e=>{ if ((e.target.tagName+e.target.type).toLowerCase()=="inputpassword"){ var p1=qs('#pass').value, p2=qs('#c_pass').value; var btnstyle=qs('#go').style; with (qs('.msg')) if(p1!=p2){ innerHTML='passwords differ ...'; btnstyle.visibility='hidden'; } else { if (p1.length>6) { innerHTML=''; btnstyle.visibility='visible' ;} else innerHTML='password must be longer 6 characters' } }}); 
 .msg {color:red; font-size:0.8em} #go {visibility:hidden} 
 <form name="form1"> <label>Please define your password here:<br> <input type="password" name="pass" id="pass"><br> <input type="password" name="c_pass" id="c_pass"> <input type="submit" value="submit" id="go"> </label> <div class="msg"></div> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM