Form validation problem

Z

Zvonko

Hi! T

This is the code:

<script type="text/javascript">
function Provjeri(i) {

var poruka = "";
if (i.ime.value=="") {
poruka+="* Nedostaje Ime i Prezime!\n";
i.ime.style.backgroundColor="FFDDDD";
}
else {
i.ime.style.backgroundColor="293B42";
}

if (i.username.value=="") {
poruka+="* Nedostaje Username!\n";
i.username.style.backgroundColor="FFDDDD";
}
else {
i.username.style.backgroundColor="293B42";
}
if (poruka!="") {
alert("Niste unijeli sve potrebne podatke:\n" + poruka);
return false;
}
else {
return true;
}
}
</script>

And this is HTML form:

<form action="unos_reg.jsp" method="POST" name="forma" id="forma">
<table id="tablica">
<tr>
<td align="left">
<strong>Ime i Prezime:<font color="yellow">*</font></strong>
</strong>
</td>
<td>
<input class="polje1" type="text" name="ime" id="ime"/>
</td>
</tr>
<tr>
<td align="left">
<strong> KorisniÄko ime:
<font color="yellow">*</font>
</strong>
</td>
<td>
<input class="polje1" type="text" name="username"
id="username"/>
</td>
</tr>
<td align="center">
<input class="gumb" type="submit" name="submit"
onclick="Provjeri(forma);" value="Registriraj se!"/>
</td>

Don't look at table tags, its just a segment of code.
I have two questions:
1. Everything works fine, form is validated, pop up is returned, but
after clicking OK on popup window, the page is redirected to
unos_reg.jsp. Why? It should stay on this page and wait for the form to
be corected! Any ideas?
2. In FireFox after validation text areas remain the same color (the
color is not changed!?)



Thanks
Zvonko
 
T

Thomas 'PointedEars' Lahn

Zvonko said:
This is the code:

<script type="text/javascript">
function Provjeri(i) {
[...]
if (poruka!="") {
alert("Niste unijeli sve potrebne podatke:\n" + poruka);
return false;
}
else {
return true;
}
}
</script>

And this is HTML form:

<form action="unos_reg.jsp" method="POST" name="forma" id="forma">
[...]
</tr>
<td align="center">
<input class="gumb" type="submit" name="submit"
onclick="Provjeri(forma);" value="Registriraj se!"/>
</td>

Don't look at table tags, its just a segment of code.

If you do not consider it important, why do you post it here in the first
place?
I have two questions:
1. Everything works fine, form is validated, pop up is returned, but
after clicking OK on popup window, the page is redirected to
unos_reg.jsp. Why?

Because the `submit' event is not canceled. You call your validation
function but you do not evaluate its return value in any way.
It should stay on this page and wait for the form to be corected!

It should not, *that* would be broken behavior with this code.
Any ideas?

It's always the same old thing. Why don't people bother to read _and_
understand before they code (and post)?

<form action="unos_reg.jsp" method="POST"
onsubmit="return function Provjeri(this);">
[...]
<input type="submit" value="Registriraj se!" class="gumb">
</form>

1. Never ever name or id any form control "submit", especially when you
don't need to as here. You would overwrite the HTMLFormElement object's
submit() method otherwise.

2. Your code was not Valid HTML and not Valid XHTML as well. However,
since you have not declared the `script' element's contents as CDATA
nor have escaped markup delimiters in the script code, I assumed you
misguidedly are serving XHTML as text/html and/or are not aware that
<.../> is XML syntax incompatible to HTML as it would mean <...>&gt;
there.

<http://validator.w3.org/>

2. In FireFox after validation text areas remain the same color (the
color is not changed!?)

You are relying on proprietary error-prone referencing, using IDs/names
as object references, which is not supported in the Gecko DOM.

A look into the JavaScript Console would have revealed this.


PointedEars
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top