Click to See Complete Forum and Search --> : text box info not linking to webpages


slickyboi
11-02-2003, 04:30 PM
Hi all,

I was wondering if anyone would be willing to look over my code to tell me what I am doing wrong? I am creating a webpage for when users type in thier room number it links them to specific pages in the intranet.. But it doesnt seem to be going anywhere when I type in the room number and click submit.. So I think I am coding wrong somewhere in the <form> section of my code... If can look it over and let me know what I might have done wrong I would be most appreciated.. Thank you!

<HTML>
<HEAD>
<TITLE>Untitled Page</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">


<SCRIPT LANGUAGE="JavaScript">

function validateroom(field) {
var valid = "0123456789-";
var hyphencount = 0;

if (!field.macth(/^[\d]{4}$/)) {
alert("Invalid characters in your room number.");
return false;
}
//This will allow the user to enter only 4 numbers...

for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your room number. Please try again.");
return false;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 4 digit room humber, like '1234'. Please try again.");
return false;
}
}
return true;

if(roomnumber>1200 && roomnumber<1800)
{
location.href="http://northwingmap.htm";
}
//repeat it for others...
}
if(roomnumber>2200 && roomnumber<2800)
{
location.href="http://centralwingmap.htm";
}
//repeat it for others...
}
if(roomnumber>3200 && roomnumber<3800)
{
location.href="http://southwingmap.htm";
}
//repeat it for others...
}
// End -->
</script>


</HEAD>


<form name=room onSubmit="return validateroom(this.room.value)">
Room #: <input type=text size=5 name=room>
<input type=submit value="Submit" onClick="validateroom(this.room.value)">
</form>



</BODY>
</HTML>

gil davis
11-02-2003, 04:46 PM
return true;
This statement ends the function and causes the form to submit without going through your if statements.

You need to execute your compares to change the page location. I would think that you would never have to return true, since the form doesn't really post anywhere. However, you would want to return false if you make it through your valid room checks.

slickyboi
11-02-2003, 05:59 PM
Thanks for replying so quickly Gil... I have been rackin my brain on this as you can probably tell I am no javascript guru and I truly appreciate your help... Thank you in advance!

You are right I am not posting but trying to get the function to create an action to execute what the users room number area map.

So would I need to put my if statements before return true; or all together remove my return true statement out of the equation?

gil davis
11-02-2003, 06:29 PM
So would I need to put my if statements before return true; or all together remove my return true statement out of the equation?
Since you aren't trying to post the form, I'd just simplify things a bit.

function validateroom(field) {
var valid = "0123456789-";
var hyphencount = 0;
if (!field.match(/^[\d]{4}$/))
{alert("Invalid characters in your room number.");
return false;}
for (var i=0; i < field.length; i++)
{temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1")
{alert("Invalid characters in your room number.
Please try again.");
return false;}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
{alert("The hyphen character should be used with a properly formatted 4 digit room humber, like '1234'.
Please try again.");
return false;}
if (roomnumber>1200 && roomnumber<1800)
{location.href="http://northwingmap.htm";
return true;}
if(roomnumber>2200 && roomnumber<2800)
{location.href="http://centralwingmap.htm";
return true;}
if(roomnumber>3200 && roomnumber<3800)
{location.href="http://southwingmap.htm";
return true;}
}
<form name=room onSubmit="return false">
Room #: <input type="text" size=5 name="room">
<input type="submit" value="Submit" onClick="validateroom(this.form.room.value)">
</form>
or
<form name=room onSubmit="validateroom(this.room.value);return false">
Room #: <input type="text" size=5 name="room">
<input type="submit" value="Submit">
</form>

slickyboi
11-02-2003, 06:53 PM
WOW! Thanks Gil...

That worked out great!! Your the best...

Its a lot more simplified.. Thats what I was exactly looking for..



Thanks again :D