当前位置导航:炫浪网>>网络学院>>网页制作>>JavaScript教程

javascript密码强度提示

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PasswordStrong._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input  id="reg_passwd__" name="reg_passwd__" value="" ;="" onkeyup="update_strength('reg_passwd__','reg_passwd__strength_display__')" autocomplete="off" type="password"/>
    <script type="text/javascript">
   function update_strength(input_field_id, output_display_id)
    {
    var input_field = document.getElementById(input_field_id);
    var output_display = document.getElementById(output_display_id);
       
    var input_str = input_field.value;
    var input_length = input_str.length;

    var strength = 0;

    // increase strength if contains [0-9]
    number_re = new RegExp("[0-9]");
    if (number_re.test(input_str)) {
      strength++;
    }
    // increase strength if contains non alpha
    non_alpha_re = new RegExp("[^A-Za-z0-9]");
    if (non_alpha_re.test(input_str)) {
      strength++;
    }

    // increase strength if contains upper case
    upper_alpha_re = new RegExp("[A-Z]");
    if (upper_alpha_re.test(input_str)) {
      strength++;
    }
   
    // increase strength if long enough
    if(input_length >=8) {
      strength++;
    }   

    var strength_str;
    if (strength <= 1) {
        strength_str = '<strong style="color:blue">弱</strong>';
    } else if (strength <= 2) {
      strength_str = '<strong style="color:blue">中等</strong>';
    } else {
      strength_str = '<strong style="color:green">强壮</strong>';
    }
   
    if(input_str.length < 6) {
      strength_str = '<strong style="color:orange">太弱</strong>';
    }

    output_display.innerHTML  = strength_str;
}
</script>
<div>
<span>密码强度: </span>
<span id="reg_passwd__strength_display__">
</span>
</div>
    </div>
    </form>
</body>
</html>

相关内容
赞助商链接