Check form

W

wj

Hi all,

Im trying to, deal with a problem that drives me crazy!

Im useing the following code to chech the fields of a form before
really submitting them:

-------
<script language="JAVASCRIPT">

function CheckForm()
{
var f = document.register;
var error_mes = '';
var submitOK="True";
if (f.place.value.length < 3)
{
error_mes = error_mes + "\n - No place.";
SubmitOK = "False";
}
if (f.provi.options[f.provi.value].value == 0)
{
error_mes = error_mes + "\n - No province.";
SubmitOK = "False";
}
if (f.educa.options[f.educa.value].value == 0)
{
error_mes = error_mes + "\n - No education.";
SubmitOK = "False";
}
if (f.prof.value.length == ' ')
{
error_mes = error_mes + "\n - No profession.";
SubmitOK = "False";
}
if (f.civil.options[f.civil.value].value == 0)
{
error_mes = error_mes + "\n - No civil.";
SubmitOK = "False";
}
if (f.pass.value.length == ' ')
{
error_mes = error_mes + "\n - No password.";
SubmitOK = "False";
}
if (f.pass.value != f.pass_ch.value)
{
error_mes = error_mes + "\n - No password check.";
SubmitOK = "False";
}
if (f.email_1.value.length < 4)
{
error_mes = error_mes + "\n - No e-mail.";
SubmitOK = "False";
}
if (f.email_1.value != f.email_2.value)
{
error_mes = error_mes + "\n - No e-mail check.";
SubmitOK = "False";
}
if (SubmitOK=="False")
{
alert(error_mes);
return false
}
}
</script>
---------

When i leave all the field empty and don't use the dropdown-boxes,
everything works fine; I get an error message and the form is not
submitted. But when i enter text or use the first three or four field
and leave the others blank, i don't get an errormessage and the data
is submitted.
Can anyone tell me how to deal with it??

Thanx!!
WJ
 
I

Ivo

if (f.prof.value.length == ' ')
if (f.civil.options[f.civil.value].value == 0)
if (f.pass.value.length == ' ')

The length property always returns a number, sometimes 0, sometimes more. A
value is always a string, sometimes an empty string (of length 0), sometimes
a string which represents a number, such as "0" or "123". You probably want
to change the above lines to

if (f.prof.value.length == 0)
if (f.civil.options[f.civil.value].value == '')
if (f.pass.value.length == 0)

HTH
Ivo
 
T

Thomas 'PointedEars' Lahn

wj said:
<script language="JAVASCRIPT">

function CheckForm()
{
var f = document.register;

function CheckForm(f)
{
f = f.elements;
if (f)
{
var error_mes = '';
var submitOK="True";

var submitOK = true;
if (f.place.value.length < 3)

if (f['place'].value.length < 3)

{
error_mes = error_mes + "\n - No place.";
SubmitOK = "False";

error_mes += "\n - No place.";
SubmitOK = false;
}
if (f.provi.options[f.provi.value].value == 0)

if (f['provi'].options[f['provi'].selectedIndex].value == 0)
{
error_mes = error_mes + "\n - No province.";
SubmitOK = "False";

See above.
}
if (f.educa.options[f.educa.value].value == 0)

if (f['educa'].options[f['educa'].selectedIndex].value == 0)
[...] {
error_mes = error_mes + "\n - No education.";
SubmitOK = "False";

See above.
}
if (f.prof.value.length == ' ')

`length' is a number property and as such never equals ' '.
You probably meant

if (f['prof'].value.length == 0)

where f['prof'].value would be the *empty* string ('').
{
error_mes = error_mes + "\n - No profession.";
SubmitOK = "False";

See above.
}
if (f.civil.options[f.civil.value].value == 0)

See above.
{
error_mes = error_mes + "\n - No civil.";
SubmitOK = "False";

See above.
}
if (f.pass.value.length == ' ')

See above.
[...]
if (SubmitOK=="False")

if (! SubmitOK)
{
alert(error_mes);
}
}
return SubmitOK;
}

</script>
---------

<form action="..." name="register" onsubmit="return CheckForm(this)">
...
When i leave all the field empty and don't use the dropdown-boxes,
everything works fine; I get an error message

What error message that was may have been important.
and the form is not submitted.

Of course.


PointedEars

P.S.
The word "I" is always capitalized in English.

P.P.S.
Your From: address is invalid:
| 550 5.1.1 <[email protected]>... User unknown
 
T

Thomas 'PointedEars' Lahn

Thomas said:
wj said:
var error_mes = '';
var submitOK="True";
^^^^^^^^

var submitOK = true; ^^^^^^^^
if (f.place.value.length < 3)


if (f['place'].value.length < 3)


{
error_mes = error_mes + "\n - No place.";
SubmitOK = "False";
^^^^^^^^

error_mes += "\n - No place.";
SubmitOK = false;
^^^^^^^^
Note that ECMAScript and its implementations are case-sensitive.
If should be *always* either `SubmitOK' or `submitOK'.


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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top