jQuery密码生成器 [英] jQuery password generator

查看:96
本文介绍了jQuery密码生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JS代码,可检查密码强度并创建随机密码.我想做的是编辑代码,以便与其将生成的密码放在password字段内,不如将生成的密码放在span标记内,并说一个id为randompassword.另外,我希望它默认情况下在span标记内有一个随机密码,然后当用户单击该按钮时,它将生成另一个密码.并将链接移动到span标签而不是密码框旁边.

I have the following JS code that checks a password strength and also creates a random password as well. What I want to do is edit the code so that instead of putting the generated password inside the password field it will put it inside a span tag with say an id of randompassword. In addition that I would like it so that by default there will be a random password inside the span tag and then when the user clicks the button it will generate another one. And also move the link to be next to span tag rather than the password box.

谢谢.

这是代码:

$.fn.passwordStrength = function( options ){
 return this.each(function(){
  var that = this;that.opts = {};
  that.opts = $.extend({}, $.fn.passwordStrength.defaults, options);

  that.div = $(that.opts.targetDiv);
  that.defaultClass = that.div.attr('class');

  that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;

   v = $(this)
  .keyup(function(){
   if( typeof el == "undefined" )
    this.el = $(this);
   var s = getPasswordStrength (this.value);
   var p = this.percents;
   var t = Math.floor( s / p );

   if( 100 <= s )
    t = this.opts.classes.length - 1;

   this.div
    .removeAttr('class')
    .addClass( this.defaultClass )
    .addClass( this.opts.classes[ t ] );

  })
  .after('<a href="#">Generate Password</a>')
  .next()
  .click(function(){
   $(this).prev().val( randomPassword() ).trigger('keyup');
   return false;
  });
 });

 function getPasswordStrength(H){
  var D=(H.length);
  if(D>5){
   D=5
  }
  var F=H.replace(/[0-9]/g,"");
  var G=(H.length-F.length);
  if(G>3){G=3}
  var A=H.replace(/\W/g,"");
  var C=(H.length-A.length);
  if(C>3){C=3}
  var B=H.replace(/[A-Z]/g,"");
  var I=(H.length-B.length);
  if(I>3){I=3}
  var E=((D*10)-20)+(G*10)+(C*15)+(I*10);
  if(E<0){E=0}
  if(E>100){E=100}
  return E
 }

 function randomPassword() {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$_+?%^&)";
  var size = 10;
  var i = 1;
  var ret = ""
  while ( i <= size ) {
   $max = chars.length-1;
   $num = Math.floor(Math.random()*$max);
   $temp = chars.substr($num, 1);
   ret += $temp;
   i++;
  }
  return ret;
 }

};

$(document)
.ready(function(){
 $('#password1').passwordStrength({targetDiv: '#iSM',classes : Array('weak','medium','strong')});

});

推荐答案

// you can use another improved version to generate password as follows

//Define
function wpiGenerateRandomNumber(length) {

    var i = 0;
    var numkey = "";
    var randomNumber;

    while( i < length) {

        randomNumber = (Math.floor((Math.random() * 100)) % 94) + 33;

        if ((randomNumber >=33) && (randomNumber <=47)) { continue; }
        if ((randomNumber >=58) && (randomNumber <=90)) { continue; }
        if ((randomNumber >=91) && (randomNumber <=122)) { continue; }
        if ((randomNumber >=123) && (randomNumber <=126)) { continue; }

        i++;
        numkey += String.fromCharCode(randomNumber);

    }

    return numkey;

}

// Call

var myKey=wpiGenerateRandomNumber(10); // 10=length

alert(myKey);



// Output

2606923083

这篇关于jQuery密码生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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