PDA

View Full Version : Help needed with simple form input script


piers
07-22-2009, 06:12 AM
I'm very new to javascript and I'm trying to write some code for an input field that only permits the user to continue if the link they are inputting is valid..

So far I have this:

function goToPage() {
var url;
var pswd;
pswd = this.document.pswdform.pswdinput.value;
loc = "folder/" + pswd + "/" + pswd + ".html"
if (pswd != "validlink1") {
alert("\nPlease enter a valid link")
return false;
}
else fetchit(loc);
}

}
function fetchit(loc) {
var root;
window.location.href = loc;
}

and the mark up;

<form name="pswdform" action="javascript:goToPage(this)"><p><input class="form_element_textfield" name="pswdinput" value="" size="20"><br /><br /><a class="button" href="javascript:goToPage(this.form);" onclick="this.blur();"><span>Submit Link</span></a>

But I can't get it to work... has anyone got any suggestions? Thanks!

mioot
07-24-2009, 05:43 AM
I removed one extra brace in the goToPage() function. And made some changes in the html tag. It working when i provide in put as "validlink1"


<script language="javascript">
function goToPage() {
var url;
var pswd;
pswd = this.document.pswdform.pswdinput.value;
alert(pswd);
loc = "folder/" + pswd + "/" + pswd + ".html"
if (pswd != "validlink1") {
alert("\nPlease enter a valid link")
return false;
}
else fetchit(loc);
}


function fetchit(loc) {
var root;
window.location.href = loc;
}


</script>

<form name="pswdform" action=""><p><input class="form_element_textfield" name="pswdinput" value="" size="20"><br /><br /><a class="button" href="#" onclick="return goToPage();"><span>Submit Link</span></a>

piers
07-25-2009, 12:29 AM
hi mioot, thanks for the response :]

Yes it seems the function is working now, just not quite as I expected. At the moment it returns an alert for whatever the user inputs (valid or not valid) before returning a second alert if it's not valid, or connecting the user if it is.

I wonder if there's a way to avoid issuing that first alert....

rnd me
07-25-2009, 11:26 AM
I wonder if there's a way to avoid issuing that first alert....

yes, delete "alert(pswd);".

btw, this inherently flawed because anyone can view the source and see the password.

at least make the password non human readable:




<script>
function goToPage() {
var pswd=document.pswdform.pswdinput.value;
loc = "folder/" + pswd + "/" + pswd + ".html";
if (pswd != String.fromCharCode(118,97,108,105,100,108,105,110,107,49) ) {
alert("\nPlease enter a valid link");
return false;
} else {
fetchit(loc);
}
}

function fetchit(loc) {
window.location.href = loc;
}

</script>


PS:
you might also be interested in the crypto program in my signature, it can encrypt a whole page from a password.

piers
07-25-2009, 02:03 PM
yes, it's definitely a (very) low security input - but in this instance that's fine...

Thanks for the help :) and I'll check out the link....