在JavaScript中生成随机密码字符串 [英] Generating random password strings in JavaScript

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

问题描述




我需要使用Javascript在我的应用程序中为用户生成随机的字母数字密码字符串

。是否有任何链接会有相同的指示?


谢谢,

Avanti

解决方案

Avanti,

你可以创建两个函数来返回随机字母和数字,

和然后一个主要功能将创建最终密码:


< script type =" text / javascript" >

函数createPassword(){

var char1 = returnAlpha();

var char2 = returnAlpha();

var char3 = returnAlpha();

var char4 = returnAlpha();

var num1 = returnInt();

var num2 = returnInt();

var num3 = returnInt();

var num4 = returnInt();

var num5 = returnInt();

var password = char1 + char2 + num5 + num4 + char3 + num3 + num2 +

num1 + char4;

alert(password);

}


函数returnInt(){

var numb = Math.floor(Math.random()* 9);

返回麻木;

}


函数returnAlpha(){

var alpha = new

数组(a,b,c,d,e,f,g,h, ; i,j,k,1,m,n,o,p,q,r, ; S"," ; t",u,v,w,x,y,z,

var alpha_index = Math.floor (Math.random()* 26);

返回alpha [alpha_index];

}

< / script>

< / head>

< body>

< button onClick =" createPassword()">创建密码< / button>

< / body>


avanti写道:





我需要使用Javascript在我的应用程序中为用户生成随机的字母数字密码字符串

。是否有任何链接会同时指向

指针?



根据您想要的字符和数量,修改

以下内容:


< script type =" text / javascript">


function genPassword(len){

var chars =''abcdefghijklmnopqrstuvwxyz0123456789''。split( '''');

var i = chars.length;

var pwd = [];

while(len - ){

pwd [len] =字符[Math.random()* i | 0];

}

返回pwd.join('''');

}


< / script>

< button onclick =" alert(genPassword(8));">获取密码< / button>


-

Rob


avanti写道:





我需要使用Javascript在我的应用程序中为用户生成随机的字母数字密码字符串

。有没有链接会有相同的

指针?


谢谢,

Avanti


下面是我刚才写的一个脚本,它生成随机的

字母数字字符串。这个概念被广泛使用,绝对不是这个代码的小说。警告:它使用Prototype.js类构造。

您可以轻松地重写它以避免这种情况。


//

//生成随机字母数字字符串。

//

Generator = Class.create();

Generator.prototype = {


//属性。

aConsLow:[''b'',''c'','d'',''f'','' g'','''',''j'',''k'',''l'',

''m'',''n'','' p '', '' q '', '' R '', '' s '的', '' T '', '' v '', '' W '', '的' x '', '' Y ',''z''],

aConsUp:[''B'',''C'','D'',''F'',''G'', ''H'',''J'',''K'',''L'',

''M'',''N'',''P'', '' Q '', '' R '', '' S '', '' T '', '' V '', '' W '', '' X '', '' Y '', '' Z''],

aHardConsLow:[''b' ,'''','''','''','''','''','/'
''k'',''''' ,'''',''','''',''v'',''z''',

aHardConsUp:[''B'',' 'C'','D'',''F'',''G'',''H'',

''K'',''M'',' P,S,T,V,Z,

aLinkConsLow:[''h'',''l '',''r''],

aLinkConsUp:[''H'',''L'',''R''],

aVowelsLow: ['''',''e'',''我',''o'',''''',

aVowelsUp:[''A'',' 'E'',''我',''U''],

aDigits:[''''','''''''''''''''4 '',''''',''''',''7'',''8'',''9''',


//构造函数。

初始化:function(){

this.CallRandom(new Date()。getSeconds());

th is.aFormat = [this.aConsLow,this.aConsUp,this.aDigits,

this.aHardConsLow,this.aHardConsUp,this.aDigits,

this.aLinkConsLow,this .aLinkConsUp,this.aDigits,

this.aVowelsLow,this.aVowelsUp,this.aDigits];

},


//调用给定次数的Math.random()并返回最后一次调用的

//结果。

CallRandom:function(iCount){

while(iCount - 1 0){

Math.random();

--iCount;

}


返回Math.random();

},


//获取aFrom中的随机索引。 />
GetRandomIndex:function(aFrom){

返回Math.floor(this.CallRandom(new Date()。getSeconds())*

aFrom。
},


//获取aFrom中的随机项目。

GetRandomItem:function(aFrom){

返回aFrom [this.GetRandomIndex(aFrom)];

},


//生成并返回给定的密码乐ngth;

生成:函数(iLength){

var sPw ="" ;;

while(iLength 0){

sPw + = this.GetRandomItem(

this.aFormat [

this.GetRandomIndex(this.aFormat)

] < br $> b $ b);

- iLength;

}

返回sPw;

}

};


干杯!

Chad


Hi,

I need to generate random alphanumeric password strings for the users
in my application using Javascript. Are there any links that will have
pointers on the same?

Thanks,
Avanti

解决方案

Avanti,
You can create 2 functions that will return random letters and numbers,
and then one primary function which will create the final password:

<script type="text/javascript" >
function createPassword(){
var char1 = returnAlpha();
var char2 = returnAlpha();
var char3 = returnAlpha();
var char4 = returnAlpha();
var num1 = returnInt();
var num2 = returnInt();
var num3 = returnInt();
var num4 = returnInt();
var num5 = returnInt();
var password = char1 + char2 + num5 + num4 + char3 + num3 + num2 +
num1 + char4;
alert(password);
}

function returnInt(){
var numb =Math.floor(Math.random()*9);
return numb;
}

function returnAlpha(){
var alpha = new
Array("a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v","w","x ","y","z");
var alpha_index =Math.floor(Math.random()*26);
return alpha[alpha_index];
}
</script>
</head>
<body >
<button onClick = "createPassword()">Create Password</button>
</body>


avanti wrote:

Hi,

I need to generate random alphanumeric password strings for the users
in my application using Javascript. Are there any links that will have
pointers on the same?

Depending on the characters you want and how many, modify the
following:

<script type="text/javascript">

function genPassword(len){
var chars = ''abcdefghijklmnopqrstuvwxyz0123456789''.split('''');
var i = chars.length;
var pwd = [];
while(len--){
pwd[len] = chars[Math.random()*i | 0];
}
return pwd.join('''');
}

</script>
<button onclick="alert(genPassword(8));">Get password</button>

--
Rob


avanti wrote:

Hi,

I need to generate random alphanumeric password strings for the users
in my application using Javascript. Are there any links that will have
pointers on the same?

Thanks,
Avanti

Below is a script I wrote a while ago which generates random
alphanumeric strings. The concept is widely used and definitely not
novel to this code. Warning: It uses the Prototype.js class construct.
You could easily rewrite it to avoid that though.

//
// Generates random alphanumeric strings.
//
Generator = Class.create();
Generator.prototype = {

// Properties.
aConsLow:[''b'',''c'',''d'',''f'',''g'',''h'',''j'',''k'',''l'',
''m'',''n'',''p'',''q'',''r'',''s'',''t'',''v'',''w'',''x'',''y'',''z''],
aConsUp:[''B'',''C'',''D'',''F'',''G'',''H'',''J'',''K'',''L'',
''M'',''N'',''P'',''Q'',''R'',''S'',''T'',''V'',''W'',''X'',''Y'',''Z''],
aHardConsLow:[''b'',''c'',''d'',''f'',''g'',''h'',
''k'',''m'',''p'',''s'',''t'',''v'',''z''],
aHardConsUp:[''B'',''C'',''D'',''F'',''G'',''H'',
''K'',''M'',''P'',''S'',''T'',''V'',''Z''],
aLinkConsLow:[''h'',''l'',''r''],
aLinkConsUp:[''H'',''L'',''R''],
aVowelsLow:[''a'',''e'',''i'',''o'',''u''],
aVowelsUp:[''A'',''E'',''I'',''U''],
aDigits:[''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9''],

// Constructor.
initialize:function() {
this.CallRandom(new Date().getSeconds());
this.aFormat = [this.aConsLow, this.aConsUp, this.aDigits,
this.aHardConsLow, this.aHardConsUp, this.aDigits,
this.aLinkConsLow, this.aLinkConsUp, this.aDigits,
this.aVowelsLow, this.aVowelsUp, this.aDigits];
},

// Calls Math.random() the given number of times and returns the
// result of the last call.
CallRandom:function(iCount) {
while(iCount - 1 0) {
Math.random();
--iCount;
}

return Math.random();
},

// Gets a random index in aFrom.
GetRandomIndex:function(aFrom) {
return Math.floor(this.CallRandom(new Date().getSeconds()) *
aFrom.length);
},

// Gets a random item in aFrom.
GetRandomItem:function(aFrom) {
return aFrom[this.GetRandomIndex(aFrom)];
},

// Generates and returns a password of the given length;
Generate:function(iLength) {
var sPw = "";
while(iLength 0) {
sPw += this.GetRandomItem(
this.aFormat[
this.GetRandomIndex(this.aFormat)
]
);
--iLength;
}
return sPw;
}
};

Cheers!
Chad


这篇关于在JavaScript中生成随机密码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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