/    Sign up×
Community /Pin to ProfileBookmark

ok serious hard coders..
i think this is easy for you..
is a random number generator script…
7 chars.. the question is how do i add a prefix..
like MG9837295 and to display it on
a text box!!???

greetings for all code helpers!!!!
RT

<SCRIPT LANGUAGE=”JavaScript”>

<!– Begin
num= new Array(7);
for (i=0; i<7; i++) {
num[i]=Math.floor(7*Math.random()+1);
if (num[1]==num[0]) {
while (num[1]==num[0]) {
num[1]=Math.floor(7*
Math.random()+1);
}
}
if (num[2]==num[1] || num[2]==num[0]) {
while (num[2]==num[1] || num[2]==num[0]) {
num[2]=Math.floor(7*Math.random()+1);
}
}
if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
num[3]=Math.floor(7*
Math.random()+1);
}
}
document.write(num[i]);
}
// End –>
</SCRIPT>

to post a comment
JavaScript

23 Comments(s)

Copy linkTweet thisAlerts:
@RterresauthorJul 17.2006 — OK..OK..

this is easier...

the function generates a random number

an pass value to a field box..

but does not work...

i think is just a syntax error!!!

about the way i call the function..

please help!!!!!!


<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

function coder(num){

num= new Array(7);

for (i=0; i<7; i++) {

num[i]=Math.floor(7*Math.random()+1);

if (num[1]==num[0]) {

while (num[1]==num[0]) {

num[1]=Math.floor(7*
Math.random()+1);

}

}

if (num[2]==num[1] || num[2]==num[0]) {

while (num[2]==num[1] || num[2]==num[0]) {

num[2]=Math.floor(7*Math.random()+1);

}

}

if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {

while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {

num[3]=Math.floor(7*
Math.random()+1);

}

}



document.FrontPage_Form1.code.output.value=(num[i]);



}



}

// End -->

</SCRIPT>
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — This works.
[CODE]<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function coder()
{
num=new Array(7);
document.getElementById('output').value = "";
for (i=0; i<7; i++)
{
num[i]=Math.floor(7*Math.random()+1);
if (num[1]==num[0])
{
while (num[1]==num[0])
{
num[1]=Math.floor(7*Math.random()+1);
}
}
if (num[2]==num[1] || num[2]==num[0])
{
while (num[2]==num[1] || num[2]==num[0])
{
num[2]=Math.floor(7*Math.random()+1);
}
}
if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0])
{
while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0])
{
num[3]=Math.floor(7*Math.random()+1);
}
}

document.getElementById('output').value+=(num[i]);
}
}
// End -->
</SCRIPT>
<input type="text" id="output" />
<input type="button" value="Run!" onclick="coder();"/>[/CODE]
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — If you want a prefix, change document.getElementById('output').value = ""; to document.getElementById('output').value = "[b]prefixhere[/b]";
Copy linkTweet thisAlerts:
@JMRKERJul 17.2006 — Seems awfully complex to me: :eek:

Will display new random number each time loaded.

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin
function coder(num){
var R = Math.floor(parseInt(num)*Math.random()+1);
document.write('Random number: MG'+R);
}
// End -->
//-->
</script>
</head>
<body onLoad="coder(1000000)">

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — Try giving your form input field an ID insted an using:

document.getElementById('IdOfInputBox').value = num[i];
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — The given function is complex because it only gets numbers from 1 to 7 I believe.
Copy linkTweet thisAlerts:
@RterresauthorJul 17.2006 — ok..

you are genious..

but..

one very very last wish..

how do i show it in a text field....at onload time ore something..

like a date.. the script puts the date by itself

(thats why i put the FrontPage_Form1.code

code is the name on the field...)


i think this will pass the value to the field but....

is not happenning....

the direction of 7 is bad..

i try to generate random from 0 to 9.. but just

7 numbers an 2 letters in prefix...

thanks million!!!!

RT
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — Then I would mash together both my post and swachtma's

This will generate 7 numbers from 0-9 and add a prefix.
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin
function coder(num){
var R = Math.floor(parseInt(num)*Math.random()+1);
document.getElementById('[b]output[/b]').value = ("[b]prefix[/b]"+R);
}
// End -->
//-->
</script>
</head>
<body onLoad="coder(1000000)">
<form>
<input type="text" id="[b]output[/b]" />
</form>
</body>
</html> [/CODE]

Just replace the bold prefix and make sure the element id's match. ?
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — Hmm, I see an issue here. If you are wanting 7 numbers every time this won't work, sometimes it gives you just 3 or 4, etc. Here's yet another revision.
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin
function coder(){
num=new Array(7);
document.getElementById('[b]output[/b]').value = "[b]prefix[/b]";
for (i=0; i<7; i++)
{
num[i]=Math.floor(9*Math.random()+1);
document.getElementById('[b]output[/b]').value += (num[i]);
}
}
// End -->
//-->
</script>
</head>
<body onLoad="coder()">
<form>
<input type="text" id="[b]output[/b]" />
</form>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — Wooha! very nice, I would only change one thing; externalize the onload so the code is more reusable.

Then I would mash together both my post and swachtma's

This will generate 7 numbers from 0-9 and add a prefix.
[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin
[B]window.onload = coder(1000);[/B]

function coder(num){
var R = Math.floor(parseInt(num)*Math.random()+1);
document.getElementById('[b]output[/b]').value = ("[b]prefix[/b]"+R);
}
// End -->
//-->
</script>
</head>
<body>
<form>
<input type="text" id="[b]output[/b]" />
</form>
</body>
</html> [/CODE]

Just replace the bold prefix and make sure the element id's match. ?[/QUOTE]


I love these forums
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — In the spirit of one-ups-manship here is a version that won't overwrite other window.onload handlers ?
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
}
else {
window.onload = function() {
oldonload();
func();
}
}
}

function coder(){
num=new Array(7);
document.getElementById('output').value = "prefix";
for (i=0; i<7; i++)
{
num[i]=Math.floor(9*Math.random()+1);
document.getElementById('output').value += (num[i]);
}
}

addLoadEvent(function () { coder(); });
// End -->
//-->
</script>
</head>
<body onLoad="coder()">
<form>
<input type="text" id="output" />
</form>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — Haha beautiful, I'm not familiar with that technique.

Care to explain whats happening in your last scipt? I'd like to learn.

I think I understand, but perhaps you could make me understand better. It certainly looks very useful
Copy linkTweet thisAlerts:
@RterresauthorJul 17.2006 — oh..my ...god...

this is a serious coder script...

by the description it looks like this works better....!!!

realy thanks

let me try with some other prefixes... for other forms...!!!

cause is a huge application database... that i must asign this

code...

10000 thanks!!!

RT
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — Sorry PineSolPirate and I got a little carried away I guess, if you want to elobrate on exact what the problem your having is I'll try to help. If you do that post the latest code your working from also; HTML and JS please.
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — Sure thing, here's a commented version.

I believe the original addLoadEvent came from http://simon.incutio.com/archive/2004/05/26/addLoadEvent though I found it through the first version of lightbox.
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Begin

function addLoadEvent(func) {
var oldonload = window.onload; // Select onload event
if (typeof window.onload != 'function') { // If the onload isn't holding a function
window.onload = func; // Stick the function in
}
else {
window.onload = function() { // Otherwise do all the old+the new
oldonload();
func();
}
}
}

function coder(){
num=new Array(7); // Seven element array (thats the size requested)
document.getElementById('output').value = "prefix";
for (i=0; i<7; i++) // Loop
{
num[i]=Math.floor(9*Math.random()+1);
document.getElementById('output').value += (num[i]); // Append
}
}

addLoadEvent(function () { coder(); }); // Add coder event to onload using addLoadEvent
// End -->
//-->
</script>
</head>
<body onLoad="coder()">
<form>
<input type="text" id="output" />
</form>
</body>
</html>[/CODE]
Hope that explained it a bit.
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — Wow, did I really just add the comment "Loop" to that code? I'm even ashamed of myself...think I'll go get some fresh air now. ?
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — That PineSol, is there an advantage to using this as opposed to assigning you onloads to variables?

such as: universalLogin = window.onload function(){}

Thats the way I've been doing things and so far its been working but it sounds like your method would be more scaleable as the application increases in size. I think I'll change over to using this method, it looks more stable.
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — I'm not sure on the technical benefits, but addLoadEvent is all over the place, lightbox, moo.fx, etc. so there must be something good about it ?
Copy linkTweet thisAlerts:
@swachtmaJul 17.2006 — You've sold me, I'll give it a whirl ?
Copy linkTweet thisAlerts:
@JMRKERJul 17.2006 — Quick question: ?

[CODE] num[i]=Math.floor(9*Math.random()+1);[/CODE]
Won't this only return numbers 1..9, not 0..9?

Wouldn't this return 0..9?
[CODE] num[i]=Math.floor(10*Math.random());[/CODE]
Copy linkTweet thisAlerts:
@RterresauthorJul 17.2006 — Ok. man for man this is working ..

i really appreciate the collaboarative mind of you!!!!

even with the loop label!!!!

just let me add as buddies for late contact..!!

you are my real savers!!!!

this application i did, is a mess... i made it with front page 2002 that is a kind of easy to insert asp in your web page.... but for real applications is really a pain in the ass..

cause is very limited in non programer capabilities... i will try to build further apps in php with sql directly from the flash interface that is mostly what i do..

the next step is to build some mailer.... a program added to the server, connected to the database that send some notification mails to the dbadmin with some info recently added..but well this is mostly

a web service that i will have to look for other tools.....

but for now...

i'm glad to be here illuminated for some real coders!!!!!

Truly yours

thanks

RT.
Copy linkTweet thisAlerts:
@PineSolPirateJul 17.2006 — Give JMRKER a cigar, he's right on that one ?

If you are on a unix/linux machine sendmail is a web favorite for uh, sending mail. There are lots of classes and scripts for easy use too.

I think there is even a windows port hidden somewhere in the bowels of microsofts website.
Copy linkTweet thisAlerts:
@RterresauthorJul 17.2006 — Yep.

the point for the 0-9 is great!!

what kind of cigars???

well.. is windows.. in fact...

i will try some with cold fusion or php...

or even direct in ASP what is the native code i generate

from Front Page!!! or maybe ASp-net.

thanks a lotttt!

regards for all

RT
×

Success!

Help @Rterres spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...