文本框的 asp.net onkeyup 事件

asp.net onkeyup event for textbox(文本框的 asp.net onkeyup 事件)
本文介绍了文本框的 asp.net onkeyup 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对带有文本框的 onkeyup 事件有疑问.我的功能是检查密码文本框的密码强度.所以当用户输入他的密码时,它会调用检查密码强度的功能,并在标签中显示他的密码是非常弱/弱/中/强.此外,文本框背景将根据密码的强度显示颜色.但是,当我输入密码文本框时,标签不显示任何内容,文本框也不会改变颜色.

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()" ></asp:TextBox><script type="text/javascript">函数 checkPasswordStrength(){var passwordTextbox = document.getElementById("tb_password");var 密码 = passwordTextbox.value;var specialCharacters = "!@#$%^&*_+";var passwordScore = 0;for (var i = 0; i  -1)){密码分数 += 20;}}if (/[a-z]/.test(password)){密码分数 += 20;}if (/[A-Z]/.test(password)) {密码分数 += 20;}if (password.length >= 8) {密码分数 += 20;}if (/[d]/.test(password)) {密码分数 += 20;}var 强度 = "";var backgroundColor = "";如果(passwordScore >= 100){强度 = "强"backgroundColor = "绿色";}否则如果(passwordScore >= 80){强度 = "中"backgroundColor = "黄色";}否则如果(passwordScore >= 60){强度 = "弱"backgroundColor = "红色";}别的{强度 = "很弱"backgroundColor = "栗色";}document.getElementById("lbl_passwordStrength").innerHTML = strength;passwordTextbox.style.color = "白色";passwordTextbox.style.backgroundColor = backgroundColor;}<br/><asp:Label ID="lbl_passwordStrength" runat="server"></asp:Label>

解决方案

ASP.NET 网页中包含的每个控件都必须包含唯一标识符 (ID).为了保持这种唯一性,ASP.Net 在页面呈现为 HTML 时更改控件的 ID.

这里,如果下面的控件在 中,那么 ID 很可能会更改为 ctl00$ctl00$ContentPlaceHolder$tb_password 例如.

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()"></asp:TextBox>

为了克服这个问题,您可以在标记中使用客户端模式或在javascript中使用ClientID.

<块引用>

方法一:

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()" ClientMode="Static"></asp:TextBox>

<块引用>

方法二:

var passwordTextbox = document.getElementById("<%=tb_password.ClientID%>");....document.getElementById("<%=lbl_passwordStrength.ClientID%>").innerHTML = strength;passwordTextbox.style.color = "白色";passwordTextbox.style.backgroundColor = backgroundColor;

I have an issue with onkeyup event with textbox. my function is to check password strength of the password textbox. so when a users enter his password, it will call the function of checking password strength and show in a label if his password is very weak/weak/medium/strong. Also, the textbox background will show colors according to the strength of the password. however when i type in the password textbox, the label does not show anything and the textbox does not change color.

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()"  ></asp:TextBox>
                        <script type="text/javascript">
    function checkPasswordStrength()
    {
        var passwordTextbox = document.getElementById("tb_password");
        var password = passwordTextbox.value;
        var specialCharacters = "!@#$%^&*_+";
        var passwordScore = 0;

        for (var i = 0; i < password.length; i++)
        {
            if(specialCharacters.indexOf(password.charAt(i) > -1))
            {
                passwordScore += 20;
            }
        }

        if (/[a-z]/.test(password))
        {
            passwordScore += 20;
        }

        if (/[A-Z]/.test(password)) {
            passwordScore += 20;
        }

        if (password.length >= 8) {
            passwordScore += 20;
        }

        if (/[d]/.test(password)) {
            passwordScore += 20;
        }

        var strength = "";
        var backgroundColor = "";

        if (passwordScore >= 100)
        {
            strength = "Strong"
            backgroundColor = "green";
        }

        else if (passwordScore >= 80)
        {
            strength = "Medium"
            backgroundColor = "yellow";
        }

        else if (passwordScore >= 60) {
            strength = "Weak"
            backgroundColor = "red";
        }

        else
        {
            strength = "Very Weak"
            backgroundColor = "maroon";
        }

        document.getElementById("lbl_passwordStrength").innerHTML = strength;
        passwordTextbox.style.color = "white";
        passwordTextbox.style.backgroundColor = backgroundColor;
    }

</script>
                        <br />
                        <asp:Label ID="lbl_passwordStrength" runat="server"></asp:Label>

解决方案

Every control that is included in an ASP.NET Web page must contain a unique identifier (ID). To maintain this uniqueness ASP.Net change the ID of control when the page gets rendered into HTML.

Here, if below control is inside <asp:ContentPlaceHolder> then the ID is likely to gets changed into ctl00$ctl00$ContentPlaceHolder$tb_password for example.

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()"></asp:TextBox>

To overcome this what you can do it either use Client Mode in mark-up or use ClientID in javascript.

Method 1:

<asp:TextBox ID="tb_password" runat="server" TextMode="Password" onKeyUp="checkPasswordStrength()" ClientMode="Static"></asp:TextBox>

Method 2:

var passwordTextbox = document.getElementById("<%=tb_password.ClientID%>");
.
.
.
.
document.getElementById("<%=lbl_passwordStrength.ClientID%>").innerHTML = strength;
passwordTextbox.style.color = "white";
passwordTextbox.style.backgroundColor = backgroundColor;

这篇关于文本框的 asp.net onkeyup 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)