469,641 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

Data Validation Help When Submitting a Form

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
4 1960
"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
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
Thanks everybody,

Code works great now!

Rick

Oct 24 '05 #4
Thanks everybody,

Code works great now!

Rick

Oct 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tim Meagher | last post: by
1 post views Thread by Johnny | last post: by
3 posts views Thread by Saket Mundra | last post: by
5 posts views Thread by Sun Jian | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.