/    Sign up×
Community /Pin to ProfileBookmark

Another validation problem

Hi

I am trying to stop a form being submitted if a valid location hasn’t been selected.

The code that isn’t working yet is,

<script language=”Javascript”>
function errorMessages()
{
valid=true;
if (document.form1.selectLocation.selectedIndex == 0)
{
alert (“Please select a valid location.”);
valid = false;
}
return valid;
</script>

Each time my form is just going through – how can I fix it?

Thanks

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Mar 28.2008 — How are you calling this function?
Copy linkTweet thisAlerts:
@sharon_annauthorMar 29.2008 — I am calling the function onSubmit of a form button being pressed.

<form name="form1" method="post" action="admin.asp" onSubmit="errorMessages()">

and this is the select box code,

<td align="left"><select name="selectLocation" id="selectLocation">

<option selected>Select *</option>

<option value="Higher Playing Field"> Higher Playing Field</option>

<option value=" Lower Playing Field"> Lower Playing Field</option>

<option value=" Leisure Centre Gym"> Leisure Centre Gym</option>

<option value="Swimming Pool"> Swimming Pool</option>

<option value=" Tennis Court"> Tennis Court</option>

<option value=" Athletics Track"> Athletics Track</option>

<option value=" Top Playing Field"> Top Playing Field</option>

<option value=" Sports Hall"> Sports Hall</option>

</select>

Thanks
Copy linkTweet thisAlerts:
@FangMar 29.2008 — &lt;form name="form1" method="post" action="admin.asp" onSubmit="[COLOR="Green"]return [/COLOR]errorMessages()"&gt;
Copy linkTweet thisAlerts:
@sharon_annauthorMar 29.2008 — Hi

I just noticed that myself after I posted the thread but it is still not working. I also had a "select *" missing from the top options, this is now how it looks and it isn't working.

<script language="Javascript">

function errorMessages()

{

valid=true;

if (document.form1.selectLocation.selectedIndex == 0)

{

alert ("Please select a valid location.");

valid = false;

}

return valid;

</script>

<select name="selectLocation" id="selectLocation">

<option value="Select *" selected>Select *</option>

<option value="Higher Playing Field"> Higher Playing Field</option>

<option value=" Lower Playing Field"> Lower Playing Field</option>

<option value=" Leisure Centre Gym">Leisure Centre Gym</option>

<option value=" Swimming Pool"> Swimming Pool</option>

<option value="Tennis Court">Tennis Court</option>

<option value=" Athletics Track"> Athletics Track</option>

<option value=" Top Playing Field"> Top Playing Field</option>

<option value=" Sports Hall"> Sports Hall</option>

</select>

<form name="form1" method="post" action="admin.asp" onSubmit= "return errorMessages()">

<input type="submit" name="Submit" value="Submit">

Thanks, any other ways of doing this - what I don't want is for the user to send the Select * to the database, I'm trying to catch it before it does.
Copy linkTweet thisAlerts:
@FangMar 29.2008 — your function is not closed and the form elements must be wrapped in the form element.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;return&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function errorMessages()
{
valid=true;
if (document.form1.selectLocation.selectedIndex == 0)
{
alert ("Please select a valid location.");
valid = false;
}
return valid;
}
&lt;/script&gt;

&lt;style type="text/css"&gt;

&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form name="form1" method="post" action="admin.asp" onSubmit= "return errorMessages()"&gt;
&lt;div&gt;
&lt;select name="selectLocation" id="selectLocation"&gt;
&lt;option value="Select *" selected&gt;Select *&lt;/option&gt;
&lt;option value="Higher Playing Field"&gt; Higher Playing Field&lt;/option&gt;
&lt;option value=" Lower Playing Field"&gt; Lower Playing Field&lt;/option&gt;
&lt;option value=" Leisure Centre Gym"&gt;Leisure Centre Gym&lt;/option&gt;
&lt;option value=" Swimming Pool"&gt; Swimming Pool&lt;/option&gt;
&lt;option value="Tennis Court"&gt;Tennis Court&lt;/option&gt;
&lt;option value=" Athletics Track"&gt; Athletics Track&lt;/option&gt;
&lt;option value=" Top Playing Field"&gt; Top Playing Field&lt;/option&gt;
&lt;option value=" Sports Hall"&gt; Sports Hall&lt;/option&gt;
&lt;/select&gt;
&lt;input type="submit" name="Submit" value="Submit"&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@sharon_annauthorMar 29.2008 — Thank you ever so much - it now works. I never knew that about the div, but I should have known about closing the function.

Thanks again - I've learned a lot.
Copy linkTweet thisAlerts:
@sharon_annauthorMar 29.2008 — Hi again

I've added some more to this now, and yet again there is something not quite right with it, its all to do with the second "if statement" - my guess is the syntax isn't quite right. The javascript isn't stopping the form getting submitted to database.

Its a follow on from the previous selectbox validations,

[CODE]<script language="Javascript">

function errorMessages()
{
valid=true;
if (document.form1.selectLocation.selectedIndex == 0 )
{
alert ("Please select a valid location.");
valid = false;
}
if (document.form1.selectSquad.selectedIndex <8 && document.form1.selectSession.selectedIndex >6 || document.form1.selectSquad.selectedIndex >7 && document.form1.selectSession.selectedIndex <7)
(
alert ("00:30 minute sessions only for Youth and Junior Squads and 01:00 sessions only for Senior Squads");
valid = false;
}
return valid;
}
</script>[/CODE]


Thanks.
Copy linkTweet thisAlerts:
@DokMar 29.2008 — The first parenthesis after
[CODE]if (document.form1.selectSquad.selectedIndex...
[COLOR="Red"]([/COLOR][/CODE]

should be a left curly brace.
[CODE]if (document.form1.selectSquad.selectedIndex...
[COLOR="Red"]{[/COLOR][/CODE]
Copy linkTweet thisAlerts:
@sharon_annauthorMar 30.2008 — Yes you were right. I couldn't see it myself.

Brilliant, thanks again.
×

Success!

Help @sharon_ann 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 5.6,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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