Forum Moderators: open

Message Too Old, No Replies

Submit form using enter

         

limbo

11:03 am on Feb 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To be honest I am a bit of a cut and paste clot (apologies to the purists - one day I'll find the time)

I copied this code from somewhere ages back - does what I need it to do but I would like the form to submit using enter ...

------------JS--------------

<script language="JavaScript" type="text/JavaScript">
function loadpage(target){
document.location.href=document.frm.pswd.value + ".html"
}
</script>

-----------html-------------

<form name="frm">
Client Login
<input type="password" name="pswd" size="20">
<input name="button" type="button" onsubmit="loadpage(target)" onClick="loadpage(target)" value="Proceed">
</form>

----------------------------

...any clues? All the documentaton I can find abot this relates to method="" attribute but I have no clues how to implement it without a lot of trial and error (no time)

Cheers, Limbo

Bernard Marx

1:05 pm on Feb 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The function:
Changed to window.location (also see alternative).


function loadpage(form){
window.location.href=form.pswd.value + ".html";
[blue]/* or, to remove current page from history..
window.location.replace(form.pswd.value + ".html");[/blue]
*/
}

HTML:
Changed
- button to type, submit
This will ensure that the form's onsubmit event is fired when enter is clicked, and the textbox is in focus.
- onsubmit handler goes on FORM element
- send reference to form with

this
keyword.

[pre]<form [color=brown]onsubmit="loadpage(this);return false"[/color]>
Client Login
<input type="password" name="pswd" size="20">
<input name="button" type="[color=brown]submit[/color]" value="Proceed">
</form>[/pre]

limbo

1:59 pm on Feb 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perfect. Thanks for your time Bernard

Ta, Limbo.

limbo

10:00 am on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What would be the best way to add an message to this script so if the incorrect password was entered the user receives a custom OS alert?

Bernard Marx

1:04 pm on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a whole new ballgame. More like tennis.

The point with the current script is that the password is the base filename of the target page. This is how the password is protected. The password page itself knows nothing, it just tries whatever is entered.

There are a couple of ways to approach this:

# say the password is: 'hello'

1. Dummy Image

Have an image called hello.jpg in the directory. First, we try to load the image by the same means. We have callback functions for onerror, and onload for the image, these functions can either load the page, or launch the alert.

This is fine in IE, but I have had trouble in other browsers. onerror is troublesome. I believe I had it working once, but don't know where the code is.

2. Hashed Password

I've never actually tried this.

There are a good few hashing scripts available out there. We keep the hashed password on the page. When we get the textbox entry, we hash the value, and if it matches the stored hash, then we load the page as previously, if not, then launch an alert.

Bernard Marx

1:23 pm on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have stickied you the URL for a demo of the Dummy Image version.

It's fine in IE.
In FF (and probably others), if you put in the wrong password, nothing happens.
- but it does work if you put in the right one.

limbo

3:30 pm on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again for your help, it is much appreciated - I now see where the analogy comes from.

So onto the next (inevitable) question. I have found the method you provided was great - however I'm back at square one if I use it, as the form does not submit with enter, and cook me a rabbit, I have no idea how to to get this to work now:

------------------JS

var checkPass_path = "pass/"
var checkPass_ext = ".html"
var checkPass_message = "Incorrect Password"

function checkPass(form)
{
var box = form.elements[0]
var input = box.value

if(input=="") doError()

var testim = new Image()

testim.onload = function()
{
window.location.href = checkPass_path + input + checkPass_ext
}

testim.onerror = doError
testim.src = checkPass_path + input + ".gif"

function doError(){ alert(checkPass_message); }
}
</script>

----------------html

<form>
<input type="password">
<input type="button" onclick="checkPass(this.form)" value="Proceed">
</form>

----------------

Ta, Limbo

Bernard Marx

4:12 pm on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The checking mechanism doesn't (shouldn't) have any effect on the submit/enter.

It's just that, my mind being elsewhere at the time, I neglected to use a SUBMIT button when I wrote it. Change the type to "submit", and it should be OK. All things being equal.