/    Sign up×
Community /Pin to ProfileBookmark

text box info not linking to webpages

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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 02.2003 — return true;[/quote]
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.
Copy linkTweet thisAlerts:
@slickyboiauthorNov 02.2003 — 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?
Copy linkTweet thisAlerts:
@gil_davisNov 03.2003 — So would I need to put my if statements before return true; or all together remove my return true statement out of the equation?[/quote]
Since you aren't trying to post the form, I'd just simplify things a bit.
<i>
</i>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 &lt; 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 &gt; 1) || ((field.length==10) &amp;&amp; ""+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&gt;1200 &amp;&amp; roomnumber&lt;1800)
{location.href="http://northwingmap.htm";
return true;}
if(roomnumber&gt;2200 &amp;&amp; roomnumber&lt;2800)
{location.href="http://centralwingmap.htm";
return true;}
if(roomnumber&gt;3200 &amp;&amp; roomnumber&lt;3800)
{location.href="http://southwingmap.htm";
return true;}
}

&lt;form name=room onSubmit="return false"&gt;
Room #: &lt;input type="text" size=5 name="room"&gt;
&lt;input type="submit" value="Submit" onClick="validateroom(this.form.room.value)"&gt;
&lt;/form&gt;

or
&lt;form name=room onSubmit="validateroom(this.room.value);return false"&gt;
Room #: &lt;input type="text" size=5 name="room"&gt;
&lt;input type="submit" value="Submit"&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@slickyboiauthorNov 03.2003 — WOW! Thanks Gil...

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

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



Thanks again ?
×

Success!

Help @slickyboi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...