Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedded js code gone goofy 1

Status
Not open for further replies.

nzt2001

Programmer
May 17, 2007
4
US
I have a simple HTML form(see below) that is giving me a headache. You can copy and paste the code into a text file and run it.

If you run the code you will see that my two field checks work fine when there is an error. However, when there is no error, the form should be calling my webpage(google for this example). It doesn't do anything.

If I remove the code that checks for the Upload File, everything works fine. What is it about the DOM call that causes my script to fail?

I'm getting some Advil:(


<form name="banner_ad" action=" method="post">
<b>Choose Zip Code: </b>
<input type="text" name="zip_code" maxlength = "5" />

<b>Select image file: </b><input id="file_name" name="userfile[]" type="file"/><br>
<input type="submit" value="Upload image" onClick="

// Zip code
var zip_code_txt = zip_code.value;
if (zip_code_txt.length != 5)
{
alert('Invalid Zip Code');
return false;
}

// Upload File
if (document.getElementById('file_name').value == '')
{
alert('Choose an image file to upload.');
return false;
}

return true;
">
 
Move your validation from the onclick event of the submit button to the onsubmit event of the form, like so:

Code:
<form name="banner_ad" action="[URL unfurl="true"]http://www.google.com"[/URL] method="post" [!]onsubmit="return(validation();"[/!]>

then move your script into a function called "validation".

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
[1]
><form name="banner_ad" action=" method="post">
[tt]<form name="banner_ad" action=" method="post" [red]enctype="mulitpart/form-data"[/red]>[/tt]

[2] If you put dummy text into the type-file input and validate its length (which is by itself fine), _and_ that the file does not actually exist, ie (specifically) will interrupt the submit process, but not so in moz. So you can say it is ie-specific behaviour you're seeing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top