How To: Generate password containing specific pattern

By zendainc ·

I am currently trying to create a .hta application to generate random passwords containing three random upper case letters followed by three random numbers. e.g. ABC123.

I done a bit of looking around with google and found the below code and tried to modify it for my needs however I need to be able to make sure the letters always come before the numbers. Currently I'm only able to make it generate a random password which sometimes does not even include any numbers.

I am still fairly new to VBScript and HTA. Could someone advise on how I can make this generate passwords of the format I need.

<title>Password Generator</title>
font-family:Arial, Helvetica, sans-serif;
<meta HTTP-EQUIV="REFRESH" content="0; url=#body">
<script language="VBScript"><!--

Sub Window_Onload
window.resizeto 292,352
End Sub
Sub set_allowed_characters()
dim strTemp,strExclude,x


set frm = document.forms("frmPWGen")

if frm.chkUpper.checked = true then
end if

if frm.chkNumbers.checked=true then
end if

strExclude = frm.txtExclude.value

for x = 1 to len(strExclude)
if instr(PASS_CHAR_ALLOWED,mid(strExclude,x,1)) > 0 then
PASS_CHAR_ALLOWED = replace(PASS_CHAR_ALLOWED,mid(strExclude,x,1),"")
end if

End Sub

Sub cmdGenerate_OnClick
dim x
set frm = Document.Forms("frmPWGen")

if not(isnumeric(frm.txtPWLen.value)) then
msgbox "Password Length must be numeric!"
exit sub
elseif not(isnumeric(frm.txtNumOfPW.value)) then
msgbox "Number of Passwords must be numeric!"
exit sub
end if

PASS_LEN = cint(frm.txtPWLen.value)
call set_allowed_characters

frm.txtPasswords.Value = ""
for x = 1 to frm.txtNumOfPW.value
frm.txtPasswords.value = frm.txtPasswords.Value & GeneratePassword(PASS_CHAR_ALLOWED) & vbCrLf
End Sub

Function GeneratePassword(strCharacters)
dim strTempCheck, blnPassCheck
dim strTempPass, strTempBit
dim x
set frm = document.forms("frmPWGen")

blnPassCheck = False

if frm.chkStartLEtter.checked = true then

for x = 1 to len(strTempCheck)
if instr(strCharacters,mid(strTempCheck,x,1)) > 0 then
blnPassCheck = True
end if

if blnPassCheck = False then
msgbox "You can't exclude all letters and still start with a letter!"
exit function
end if

blnPassCheck = False


strTempBit = mid(strCharacters,Int((len(strCharacters)) * Rnd + 1),1)
if instr(strTempCheck,strTempBit) > 0 then
blnPassCheck = True
end if
loop until blnPassCheck = True

strTempPass = strTempBit
end if

do until len(strTempPass) = PASS_LEN
strTempPass = strTempPass & mid(strCharacters,Int((len(strCharacters)) * Rnd + 1),1)

GeneratePassword = strTempPass
End Function
<body bgcolor="#000000" topmargin="1" leftmargin="1">
<a name="body">
<form id="frmPWGen">
<table width="280" border="1" bgcolor="#73A610" bordercolor="#000000" cellpadding="2" cellspacing="0">
<tr bgcolor="#FFFFFF"><td colspan="2"><center>
<font color="#000000" size="+1" face="Verdana, Arial, Helvetica, sans-serif">Password Generator</font>
<tr><td>Password Length:</td><td><input type="text" name="txtPWLen" value="6" size="15"></input></td></tr>
<tr><td>Number of passwords:</td><td><input type="text" name="txtNumOfPW" value="1" size="15"></input></td></tr>
<tr><td colspan="2"><center><label for="UpperCase"><b>UPPER:</b></label><input type="checkbox" name="chkUpper" id="UpperCase" checked="true"></label></input>
<label for="Numbers"><b>NUMBERS:</b></label><input type="checkbox" name="chkNumbers" id="Numbers" checked="true"></input>
<br><label for="StartLetter"><b>Start with Letter:</b></label><input type="checkbox" name="chkStartLetter" id="StartLetter" checked="true"></input>
<tr><td>Exclude:</td><td><input type="text" name="txtExclude" value="" size="15"></input></td></tr>
<tr><td colspan="2"><center><input type="button" name="cmdGenerate" value="Generate Password"></input>
<input type="reset" name="cmdReset" value="Reset"></input></center></td></tr>
<tr><td colspan="2"><center><textarea name="txtPasswords" cols="30" rows="6">Generated passwords</textarea></center></td></tr>

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Browser went crazy

by TobiF In reply to How To: Generate password ...

My favourite browser (Opera) went totally crazy on your post. It was impossible to write a reply. Had to fire up IE in order to not have a jumping page constantly reloading.

The reason is, of course, will keep some html etc from your post. That's nice when you want to enter some <b>bold</b> text etc, but makes it very hard to publish pieces of javascript and html.

I'd suggest you hang your code somwhere else (perhaps as a txt file) and give a link here.

Collapse -

if you want letters then numbers

by Tony Hopkinson In reply to How To: Generate password ...

then just generate three letters, then three numbers and just bolt them together.

This would be a lot easier if youi wrote a function that returned random one character out of a passed string then called it until you met the length requirement.
You could kill repeats then as well if you wanted simply by removing chosen characters from the input.

Another way is to create an array of valid characters, shuffle them the pick one position and take the next n from there.

Related Discussions

Related Forums