Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert a line of VBA to JS

Status
Not open for further replies.

Sam6284

Programmer
Apr 24, 2002
16
FR
I'm just learning JS. I've been able to convert all but a few lines of the original code. Can anyone help? Many thanks.

function ValidatePW(f)
{
var decryptedPW = f.ApprovalPassword.value
var offPW = f.OfficerPW.value;
var mgrPW = f.ManagerPW.value;
var pw = "";
var i, j;


// Begin VB Script

j = Len(decryptedPW) + 1
For i = 1 To Len(decryptedPW)
pw = pw & Chr((270 + i - Asc(Mid(decryptedPW, j - i, 1))) And 255)
Next i

// End VB Script


if ((pw == offPW) || (pw == mgrPW))
{
return true;
}
else
{
alert ("The password entered does not match either password.");
return false;
}
}
 
This should be something like that :
Code:
j = decryptedPW.Length();
for (var i = 0; i< decryptedPW.Length();i++) {
  var tmpLetterCode = new Number(decryptedPW.charCodeAt(i));
  pw = pw & String.fromCharCode((270 + i - tmpLetterCode) And 255);
}
Water is not bad as long as it stays out human body ;-)
 
Thanks, Targol. The charCodeAt and fromCharCode were the key.

I cut and pasted your code so the complete function now reads:

function ValidatePW(f)
{
var pw = f.ApprovalPassword.value;
var offPW = f.OfficerPW.value;
var mgrPW = f.ManagerPW.value;

for (var i = 0; i< decryptedPW.Length();i++) {
var tmpLetterCode = new Number(decryptedPW.charCodeAt(i));
pw = pw & String.fromCharCode((270 + i - tmpLetterCode) And 255);
}

if ((pw == offPW) || (pw == mgrPW))
{
return true;
}
else
{
alert (&quot;The password entered does not match either password.&quot;);
return false;
}
}

There's an error somewhere. For testing, I set both return statements to false and the action= page opened anyway. That means the function is returning true before making the if test.

The &quot;&&quot; character in the pw = pw & String.fromCharCode line caught my attention. I tried using &quot;+&quot; instead. There was no change.

Perhaps there is a problem with And 255. Perhaps there are mismatched braces or some other syntax error. Do you see the reason the function is returning true prematurely?
 
Scuse me for the mistakes. Logical AND in JS is &quot;&&&quot; while binary AND is &quot;&&quot; Water is not bad as long as it stays out human body ;-)
 
for (var i = 0; i< decryptedPW.Length();i++) {

should be

for (var i = 0; i< decryptedPW.length;i++) {

remember that js is case-sensitive, and [tt]length[/tt] is a property, not a method.
=========================================================
if (!succeed) try();
-jeff
 
Targol,

Thanks for the explanation of &quot;+&quot; vs. &quot;&&&quot; vs. &quot;&&quot;. I'm pretty sure I know how the pw = pw ... line should now read.

Jemminger,

That level of detail is exactly what I appreciate so much in this forum. It took me a while to get used to VB/VBA editors capitalizing everything. Now I'm having trouble going back.

So on this method vs. property, - String.charFromCode from this example - Methods are always capped and properties are always LC? That seems easy enough to remember.

Many thanks to both of you for your help on this.

samr.
 
As all jscript languages are made from Java, they respect naming conventions for this language :
Classes begins with Ucase and use Polish notation (one Upper for each &quot;new word&quot; in the name)
exemple : &quot;StringTokenizer&quot;, &quot;Date&quot;, &quot;ActiveX&quot;, ...
Methods unlike VB, does not make any diference between function and subs : all methods should return something (void is something) They begins with Lcase, use Polish notation and are always folowed by parenthesis (even if no params)
exemple : &quot;getElementById()&quot;, &quot;toString()&quot;, &quot;createElement()&quot;, ...
Properties begins with Lcase, use Polish notation (in general)
exemple : &quot;bottomMargin&quot;, &quot;clientWidth&quot;, ...
Water is not bad as long as it stays out human body ;-)
 
samr,

close: properties store values, and do not end with parentheses.

methods act on Objects and end in parentheses, and sometimes require arguments.

typically properties and methods will begin lowercase, and have subsequent words within the name capitalized:

length;
location.href;
someObject.someProperty;

document.getElementById(&quot;someId&quot;);
history.go(-1);
someObject.someMethod(args);

Objects typically begin in uppercase, but only if you're using a class reference, like Math.round();
instance references would be lowercase, like the current Window object is accessed using window.location, window.document etc...

hope this doesn't confuse too much!

=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top