Jump to content

[SOLVED] Filling in Form Input


FlyingIsFun1217

Recommended Posts

Hey, here's what I've got. Seems to work in IE, but not Firefox. Any clue?

 

<script language="javascript">
				function random_passwd()
				{
					chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*";
					password = "";
					for(x=0;x<9;x++)
					{
						i = Math.floor(Math.random() * 79);
						password += chars.charAt(i);
					}

					return password;
				}

				function formSubmit()
				{
					generatorForm.passwdBox.value = random_passwd();
					return false;
				}
			</script>
			<form name="generatorForm">
				<input name="passwdBox" type="text" />
				<input type="button" value="Generate New Password" onClick="javascript:formSubmit()" />
			</form>

 

Seems that it works in Chrome, IE, but not Firefox. Is there some little Javascript snippet that Firefox does not support currently?

 

Thanks!

FlyingIsFun1217

Link to comment
Share on other sites

Try:

<input type="button" value="Generate New Password" onClick="formSubmit()" />

without the javascript infront of it.

 

If it still doesn't work, try use alert statement to debug which line the statement is not working, eg:

 

               function formSubmit()
               {
                  alert("This function is called.");
                  generatorForm.passwdBox.value = random_passwd();
                  return false;
               }

Link to comment
Share on other sites

It works for me in FF, but there are some problems in the code. You only have a string of 70 characters, yet you are generating random numbers from 0 to 78 (i.e. 79 characters). The function should be dynamic based upon the number of characters in the string.

 

I would also make the length of the password dynamic with a min and max length, say from 9 to 15 characters. Also, you don't need to return false when using a non-submit button.

 

<script language="javascript">
function random_passwd(min, max)
{
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*';
var length = min + Math.floor(Math.random() * (max-min))
var password = '';

for(var i=0; i<length; i++)
{
	password += chars.charAt(Math.floor(Math.random() * chars.length));
}
return password;
}

function formSubmit()
{
generatorForm.passwdBox.value = random_passwd(9, 15);
return false;
}
</script>

<form name="generatorForm">
<input name="passwdBox" type="text" />
<input type="button" value="Generate New Password" onClick="formSubmit()" />
</form>

Link to comment
Share on other sites

I've uploaded my example to http://www.ploronex.com/it109/password.html. I still can't see what's causing a problem at all, and it seems when I change 79 to a lower value, it doesn't work in other browsers. Man, this is starting to confuse me. Guess that's what I get when I don't know javascript well.

 

Thanks for any further guidance, I appreciate it!

 

FlyingIsFun1217

Link to comment
Share on other sites

OK, the problem is how you are referencing your input object. You are not stating it in the appropriate context. It doesn't know where to find the "generatorForm" object. You should use:

document.generatorForm.passwdBox.value = random_passwd();

 

I see no reason why that code would not work with numbers lower than 79. Besides, as I stated you don't need to hard code that value, just use the length of the variable with the characters.

 

Full code that should work:

				<h3>Secure Password Generator</h3>
			<script language="javascript">
				function random_passwd(min, max)
				{
					var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*';
					var length = min + Math.floor(Math.random() * (max-min))
					var password = '';

					for(var i=0; i<length; i++)
					{
						password += chars.charAt(Math.floor(Math.random() * chars.length));
					}
					return password;
				}
			</script>
			<form name="generatorForm">
				<input name="passwdBox" type="text" value=""/>
				<input type="button" value="Generate New Password" onClick="this.form.passwdBox.value = random_passwd(9, 13);" />
			</form>

Link to comment
Share on other sites

Not to be rude, but why should it fix your mistakes? Just because IE makes an assumption of what it thinks you want and it works as you would like doesn't make it valid code.

 

Because it is invalid code there is no standard on how it should be interpreted. The browsers that "work" with your code may have different logic for "fixing" these types of errors and it just happened, in this instance, that they got it right - this time. It could just as well of been that each could have interpreted the invalid code differently resulting in wildly different results.

 

Think about all the time you have wasted on this because you developed in one application that interprets something differently than another application. If they all follow the same standard you don't need to do as much testing between browsers. I for one would rather have an application not work with invalid code - making me fix it - than having it work with my code but not working in the other applications.

Link to comment
Share on other sites

Not to be rude, but why should it fix your mistakes? Just because IE makes an assumption of what it thinks you want and it works as you would like doesn't make it valid code.

 

I'm not saying it should, I actually think the other browsers are "in the wrong", so to speak. My only interest is that every other browser seems to fix it. Makes it seem like Firefox is not doing things right when it works with everything else.

 

FlyingIsFun1217

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.