views:

3195

answers:

7

Hi there,

I'm working on a simple javascript login for a site, and have come up with this:

<form id="loginwindow">
<strong>Login to view!</strong>
<p><strong>User ID:</strong>
  <input type="text" name="text2">
</p>
<p><strong>Password:</strong>
<input type="password" name="text1"><br>
  <input type="button" value="Check In" name="Submit" onclick=javascript:validate(text2.value,"username",text1.value,"password") />
</p>

</form>
<script language = "javascript">

function validate(text1,text2,text3,text4)
{
 if (text1==text2 && text3==text4)
 load('album.html');
 else 
 {
  load('failure.html');
 }
}
function load(url)
{
 location.href=url;
}
</script>

...which works except for one thing: hitting enter to submit the form doesn't do anything. I have a feeling it's cause I've used "onclick" but I'm not sure what to use instead. Thoughts?

A: 

My Thought = Massive security hole. Anyone can view the username and password.

More relevant to your question: - You have two events happening.

  1. User clicks button.
  2. User presses enter.

The enter key submits the form, but does not click the button.

By placing your code in the onsubmit method of the form the code will run when the form is submitted. By changing the input type of the button to submit, the button will submit the form in the same way that the enter button does.

Your code will then run for both events.

Ady
+11  A: 
keparo
this looks pretty darn good. couple quick questions: 1. where in this code do i set the username and password?2. would it help with security if i were to put the javascript in an external file and link to it?thank you so much for your thorough answer!
3. (haha sorry for being annoying. i really appreciate your help!) it seems as though using your code, it doesn't matter what i enter for the username and password, it just redirects to album.html. how can i fix that?
keparo
Re: 2 Putting the js in an external file won't help. The secure way is to allow the form to submit the values to the server over https, and control the login on the server. It's a bit much to explain entirely as an aside to this question, but I can recommend some resources or open a new question..
keparo
+1  A: 

Instead of <input type="button">, use <input type="submit">. You can put your validation code in your form onsubmit handler:

<form id="loginwindow" onsubmit="validate(...)">

Josh Hinman
A: 

Maybe you can try this:

<form id="loginwindow" onsubmit='validate(text2.value,"username",text1.value,"password")'>
<strong>Login to view!</strong>
<p><strong>User ID:</strong>
   <input type="text" name="text2">
</p>
<p><strong>Password:</strong>
<input type="password" name="text1"><br>
   <input type="submit" value="Check In"/>
</p>

</form>

As others have pointed out, there are other problems with your solution. But this should answer your question.

Vincent Ramdhanie
+1  A: 

it's because it's not a form submitting, so there's no event to be triggered when the user presses enter. An alternative to the above form submit options would be to add an event listener for the input form to detect if the user pressed enter.

<input type="password" name="text1" onkeypress="detectKey(event)">
Zurahn
A: 

dang, fast replies guys!

okay yeah so I'm well aware of how flimsy this is security-wise. It's not for anything particularly top secret, so it's not a huge issue, but if you guys could elaborate on your thoughts with code, I'd love to see your ideas. the code i listed is literally all i'm working with at this point, so i can start from scratch if need be.

A: 

Surely this is too unsecure as everyone can crack it in a second ...

-- only pseudo-secure way to do js-logins are the like:

<form action="http://www.mySite.com/" method="post" onsubmit="this.action+=this.theName.value+this.thePassword.value;">
  Name: <input type="text" name="theName"><br>
  Password: <input type="password" name="thePassword"><br>
  <input type="submit" value="Login now">
</form>
roenving