PDA

View Full Version : a few variables and function, I want javascript conversion


Zeeshan Arshad
06-27-2005, 11:45 AM
Hi Guys,

This is a script designed in BASIC, I want to know its javascript compatible code, can you convert this?

This illustrate 3 TEXT BOXES (edit1, edit2, edit3) which are being used for this example.

--------------------------
Sub RegMe()
Temp1 = 0: Temp3 = 0: Temp4 = 0
Nam$ = Edit1.Text
Temp2 = Val(Edit2.Text)

For Temp1 = 1 To Len(Nam$) 'Gets each char of name
Temp3 = Asc(Mid$(Nam$, Temp1, 1)) 'converts each char to ASCII
Temp4 = Temp4 + Temp3 'adds each ASCII value
Next

Temp5 = Temp4 * 6 'Multiplies final ASCII by 6
Temp6 = Temp2 + Temp5 'Actual Number Of Registration Code
Edit3.Text = Str$(Temp6) 'Final Result

End Sub
---------------------

please convert and post javascript style solution, and I would appreciate it.

much Thanks :)

nikkiH
06-27-2005, 01:28 PM
Did you even attempt to do this yourself?
How about you try that, and when you run into trouble, we'll help you debug it.

martin_narg
06-27-2005, 01:29 PM
Is this something like you are looking for?

It only works for a-z 0-9 and pretty much regular characters. the list can be extended for use with whitespace/linefeeds/etc.

Also if you were using a different character set other than This, then the list can also be extended to encompass these.

Some wider reading can be found here:
http://www.w3schools.com/html/html_asciiref.asp


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Convert to ascii</title>
<script>
function ascii(strInput, objOutput) { // pass up input string and output form object
var i, o, bln, strOutput;
strOutput = "";

var strChars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

for( i=0; i<strInput.length; i++ ) {
bln = false;
char_loop:
for( o=0; o<strChars.length; o++ ) {
if( strInput.charAt(i) == strChars.charAt(o) ) {
bln = true;
strOutput += String("&#"+(32+o)+";"); // my ascii character list starts at ascii character code 32
break char_loop;
}
}
if(!bln) {
strOutput += strInput.charAt(i); // if character not found in - output the original character
}
}

objOutput.value = strOutput;
}
</script>
<style>
body {
font-family: "Courier New", Courier, mono;
font-size: 12px;
color: #000;
font-weight: bold;
}

input {
font-family: "Courier New", Courier, mono;
font-size: 12px;
color: #000;
}

textarea {
font-family: "Courier New", Courier, mono;
font-size: 12px;
color: #666;
}
</style>
</head>

<body>
<form name="frm">
Input:<br>
<input type="text" name="txtInput" size="50">

<br><br>
Output:
<br>
<textarea name="txtOutput" cols="50" rows="5"></textarea>

<br><br>
<input type="button" value="convert to ASCII" onclick="ascii(document.frm.txtInput.value, document.frm.txtOutput);">
</form>
</body>
</html>


Hope this helps - tested in IE, Firefox and Opera

m_n

Zeeshan Arshad
06-28-2005, 10:37 AM
Thanks Martin, I will check this.

Also, actually I only want this to be done via javascript.

VB/Script Solution = Javascript Solution
-----------------------------------------
Len() = ???
Asc() = ???
Mid$() = ???


so only three functions within Javascript (AND also with PHP if you know)

meanwhile I will search on the net for this one..

Thanks.

martin_narg
06-28-2005, 01:47 PM
The code I posted is all JS and HTML.

Your questions with regards to the string methods can be answered here:
http://www.devguru.com/Technologies/ecmascript/quickref/string.html

PHP documentation here:
http://www.php.net/docs.php

m_n