By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,414 Members | 2,903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,414 IT Pros & Developers. It's quick & easy.

Data Validation Help When Submitting a Form

P: n/a
Hello,

I'm having trouble with submitting my form when checking to see if data
is present in the user-inputted fields. What I want to happen is for
the user to input various pieces of data, submit the form, and then
have a javascript function that checks to see if data is entered, and
if not, have an alert window tell the user which field to enter data
into and then revert back to that same page. If all data is entered,
it would submit the form and send the user to another page. However,
I'm having trouble with reverting back to the orginal page for the user
to update fields to. Here is my code for all other working parts:

<html>
<head>
<script type="text/javascript">

function main(form)
{
if(form.description.value =='')
{
alert("you have not entered in a description");
}
if(form.awarddate.value =='')
{
alert("you have not entered in an award date");
}
}
</SCRIPT>
</head>
<body>
<form name="input" action="submit.html" method="post">
<table>
Rick's Test Page</th><br><br>
<tr>
<td>Description:<td><input type=text name="description" id=1>
</tr><tr>
<td>Award Date:<td><input type=text name="awarddate" id=2>
</tr>
</table>
<input type="submit" class="FormText" value="submit"
onClick="main(input)">
</form>
</body>
</html>

Any ideas?...Thanks!

Rick

Oct 24 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Rick" <RS********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello,

I'm having trouble with submitting my form when checking to see if data
is present in the user-inputted fields. What I want to happen is for
the user to input various pieces of data, submit the form, and then
have a javascript function that checks to see if data is entered, and
if not, have an alert window tell the user which field to enter data
into and then revert back to that same page. If all data is entered,
it would submit the form and send the user to another page. However,
I'm having trouble with reverting back to the orginal page for the user
to update fields to. Here is my code for all other working parts:


[snip]

Will this help? Watch for word-wrap.

<html>
<head>
<script type="text/javascript">
function main(form) {
if (form.description.value =='') {
alert("'Description' is missing.");
return false;
} else if (form.awarddate.value =='') {
alert("'Award Date' is missing.");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="submit.html" method="post"
name="input" onsubmit="return main(this)">
<table>
<tr>
<th colspan="2">Rick's Test Page<br><br></th>
</tr>
<tr>
<td>Description: <input type="text" name="description"></td>
</tr>
<tr>
<td>Award Date: <input type="text" name="awarddate"></td>
</tr>
</table>
<br><br>
<input type="submit" value="Submit" class="FormText">
</form>
</body>
</html>
Oct 24 '05 #2

P: n/a
Rick wrote on 24 okt 2005 in comp.lang.javascript:
Hello,

I'm having trouble with submitting my form when checking to see if data
is present in the user-inputted fields. What I want to happen is for
the user to input various pieces of data, submit the form, and then
have a javascript function that checks to see if data is entered, and
if not, have an alert window tell the user which field to enter data
into and then revert back to that same page. If all data is entered,
it would submit the form and send the user to another page. However,
I'm having trouble with reverting back to the orginal page for the user
to update fields to. Here is my code for all other working parts:

<html>
<head>
<script type="text/javascript">

function main(form)
do not use a reserved name as a variable
{
if(form.description.value =='')
{
alert("you have not entered in a description");
}
if(form.awarddate.value =='')
{
alert("you have not entered in an award date");
}
}
</SCRIPT>
give a return false if the form should not be submitted yet
</head>
<body>
<form name="input" action="submit.html" method="post">
put asn onsubmit here
<table>
Rick's Test Page</th><br><br>
<tr>
<td>Description:<td><input type=text name="description" id=1>
</tr><tr>
<td>Award Date:<td><input type=text name="awarddate" id=2>
</tr>
</table>
<input type="submit" class="FormText" value="submit"
onClick="main(input)">
if the fields are submitted by giving enter the main() will not be
invoked
</form>
</body>
</html>


Try this lean code,
that will only submit if both input fields are filled:
<html>
<head>

<script type="text/javascript">

function main(x) {
var alertstring = '';
if(x.description.value =='')
alertstring = 'you have not entered in a description.';
if(x.awarddate.value ==''){
alertstring += '\nyou have not entered in an award date.';

if (alertstring != ''){
alert(alertstring + '\nI need both fields filled!');
return false;
}
return true;
};

</script>

</head>
<body>

<form action="submit.html" method="post"
onsubmit="return main(this)">

Description: <input name="description">
<br>
Award Date: <input name="awarddate">
<br>
<input type="submit" value="Submit me">
</form>

</body>
</html>

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 24 '05 #3

P: n/a
Thanks everybody,

Code works great now!

Rick

Oct 24 '05 #4

P: n/a
Thanks everybody,

Code works great now!

Rick

Oct 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.