谁能告诉我该代码的解决方案 [英] can any body tell me the solution for this code

查看:48
本文介绍了谁能告诉我该代码的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"   OnTextChanged="javascript:validatePwd();" CausesValidation="True"  AutoPostBack="true" MaxLength="12">



在这段代码中,我想调用"validatePwd()",其定义如下...



in this code i wanted to call "validatePwd()" which is defined as below...

<script type="text/javascript" language="javascript">

    function validatePwd()
     {
        var invalid = " "; // Invalid character is a space
        var minLength = 6; // Minimum length

        var pw1 = document.getElementById('txtPwd');
        var pw2 = document.getElementById('txtRePwd');

        // check for a value in both fields.

        if (pw1 == '' || pw2 == '') {
            alert('Please enter your password twice.');
            return false;
        }
        // check for minimum length
        if (document.getElementById('txtpwd'.length()) < minLength) {
            alert('Your password must be at least ' + minLength + ' characters long. Try again.');
            return false;
        }
        // check for spaces
        if (document.getElementById('txtpwd'.indexOf(invalid)) > -1) {
            alert("Sorry, spaces are not allowed.");
            return false;
        }
        else {
            if (pw1 != pw2) {
                alert("You did not enter the same new password twice. Please re-enter your password.");
                return false;
            }
            else {
                alert('PASSWORD MATHCHED');
                return true;
            }
        }
    }

</script>

推荐答案

尝试一下,我已经更改了您的JavaScript
Try this, i have changed your JavaScript
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"  MaxLength="12">

</asp:TextBox>
<asp:TextBox ID="txtRePwd" runat="server" TextMode="Password"  MaxLength="12">

</asp:TextBox>
<input type='button' onclick="return validatePwd();" />

<script type="text/javascript" language="javascript">
    function validatePwd() {
        var invalid = " "; // Invalid character is a space
        var minLength = 6; // Minimum length

        var pw1 = document.getElementById('<%=txtPwd.ClientID%>').value;
        var pw2 = document.getElementById('<%=txtRePwd.ClientID%>').value;

        // check for a value in both fields.

        if (trim(pw1) == '' || trim(pw2) == '') {
            alert('Please enter your password twice.');
            return false;
        }
        else
        // check for minimum length
        if (pw1.length < minLength) {

            alert('Your password must be at least ' + minLength + ' characters long. Try again.');

            return false;

        }

        // check for spaces

                else {

                    if (pw1.toUpperCase() != pw2.toUpperCase()) {

                alert("You did not enter the same new password twice. Please re-enter your password.");

                return false;

            }

            else {

                alert('PASSWORD MATHCHED');

                return true;

            }

        }

    }

    //function to trim the text

    function trim(str) { return str.replace(/^\s*|\s*


/g, "); } </script >
/g, ""); } </script>


请参阅
http://blog.stevenlevithan.com/archives/javascript-password-validator [ ^ ]
您的代码包含很多错误
1.您没有使用trim
2.长度不是这样使用的
3.在比较密码时,您没有将大小写转换成大小写.
4.如果使用的是runat服务器,并且页面包含母版页,则使用ClientID获取文本框值.
Refer this
http://blog.stevenlevithan.com/archives/javascript-password-validator[^]
Your code contains lots of error
1. You are not using trim
2. Length is not used like this
3. While comparing password, you are not converting then to same case either lower or upper.
4. If you are using runat server and your page contains a master page, then use ClientID to get the textbox values.


这篇关于谁能告诉我该代码的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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