Catches error but still submits

R

roman

Hi,

I have a form which validates data before submitting, it catches the
error, but still submits and can't seem to figure out why. Can someone
look at my code and give me some ideas how to make it work? I need to
be able to submit by clicking on button and also by pressing on the
Enter key.

Here are my results

Internet Explorer 5. :
Enter Key: works fine
Send: catches error but sumbits

Firefox 1.07 :
Enter Key: catches error but sumbits
Click Send: catches error but sumbits


Thanks,
Roman

Code is here:

<html><head><title>Starting Point</title></head>

<script>
<!-- Hide script


function verifyData(TicketForm, FromOnClick) {

alert ("in verifyData");

if (FromOnClick) {
alert ("FromOnClick");
return true;
}


if (TicketForm.YourName.value == "" ) {
alert ("Please enter your Name!");
return false;
}

return true;

}
// End script hiding -->
</script>


<body>

<form action="Ending.html" method="post" enctype="multipart/form-data"
name="StartingPoint" onsubmit="return verifyData(this,false)">


<b>Your Name Is: </b>
<input name=Your Name id="YourName" size="15" value="">

<b> <input onclick="if( verifyData(document.forms.StartingPoint,true)
){document.StartingPoint.submit();}" value="Send"
type="submit"></b></font>


</form>
</body>
</html>

==== The Ending.html code is here:

<html><head><title>Ending Point</title></head>
<body>

<b> Thanks ! <b>

</body></html>
 
T

TheBagbournes

roman said:
Hi,

I have a form which validates data before submitting, it catches the
error, but still submits and can't seem to figure out why. Can someone
look at my code and give me some ideas how to make it work? I need to
be able to submit by clicking on button and also by pressing on the
Enter key.

Here are my results

Internet Explorer 5. :
Enter Key: works fine
Send: catches error but sumbits

Firefox 1.07 :
Enter Key: catches error but sumbits
Click Send: catches error but sumbits


Thanks,
Roman

Code is here:

<html><head><title>Starting Point</title></head>

<script>
<!-- Hide script


function verifyData(TicketForm, FromOnClick) {

alert ("in verifyData");

if (FromOnClick) {
alert ("FromOnClick");
return true;
}


if (TicketForm.YourName.value == "" ) {
alert ("Please enter your Name!");
return false;
}

return true;

}
// End script hiding -->
</script>


<body>

<form action="Ending.html" method="post" enctype="multipart/form-data"
name="StartingPoint" onsubmit="return verifyData(this,false)">


<b>Your Name Is: </b>
<input name=Your Name id="YourName" size="15" value="">

<b> <input onclick="if( verifyData(document.forms.StartingPoint,true)
){document.StartingPoint.submit();}" value="Send"
type="submit"></b></font>

A problem is that the <input type="submit"> will submit *without* the
onclick. So your onclick is correctly not submitting, but then the
<input> itself is doing its thing and submitting. Try just returning the
value from verifyData() in the onclick.

I don't know why the the Enter key onsubmit returning false wouldn't
abort a submit. More investigation needed.
 
M

Michael Winter

On 11/03/2006 16:26, roman wrote:

[snip]
Code is here:

A live example is usually preferred.
<html><head><title>Starting Point</title></head>

HTML documents should contain a document type declaration (DOCTYPE).

The type attribute is required:

<!-- Hide script

This hasn't been necessary for years. Drop it.

[snip]
if (TicketForm.YourName.value == "" ) {

If you want to check for empty values, a regular expression test for
white space might be better:

function verifyData(form) {
var controls = form.elements;

if (/^\s*$/.test(controls.YourName.value)) {
alert('Please enter your name.');
return false;
}
}

[snip]
// End script hiding -->

That should go as well, obviously.

[snip]
<b>Your Name Is: </b>
<input name=Your Name id="YourName" size="15" value="">

You should be using labels, and that name attribute value /must/ be quoted:

<label>Your name:
<input name="YourName" size="15" value="">
</label>

If you want the label text to be bold, style it as such using CSS.
<b> <input onclick="if( verifyData(document.forms.StartingPoint,true)
){document.StartingPoint.submit();}" value="Send"
type="submit"></b>

Your problem is here. Calling the submit method triggers an
unconditional submission. The method call isn't necessary anyway: just
let the submit button do it's job.

?

Not only is there no starting font tag, the font element has long been
deprecated, has long been considered evil, and should be replaced by CSS.

[snip]

Hope that helps,
Mike
 
T

Thomas 'PointedEars' Lahn

Michael said:
If you want to check for empty values, a regular expression test for
white space might be better:

function verifyData(form) {
var controls = form.elements;

if (/^\s*$/.test(controls.YourName.value)) {

if (!/\S/.test(controls.YourName.value)) {

suffices, and is more efficient.


PointedEars
 
R

roman

Thanks Michael for the reply.

This works fine locally; now I need to figure out why when I put it on
the web server, pressing the Enter key on a text field, and all data is
valid (no errors), it resets all text fields and the form is not
submitted.

Thanks again,
Roman
 

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

No members online now.

Forum statistics

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

Latest Threads

Top