Data Validation Help When Submitting a Form

R

Rick

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
 
M

McKirahan

Rick said:
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>
 
E

Evertjan.

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top