Forum Moderators: open

Message Too Old, No Replies

form validation: checking input fiel if checkbox is checked

how to prevent inputfield from being empty when checkbox is checked

         

Darkelve

8:35 am on Mar 3, 2005 (gmt 0)

10+ Year Member



Hi,

I want to make sure that, when a certain checkbox is checked, the following input field (text field) cannot be empty.

The code I have so far doesn't work, however. (x is the variable holding the name of my form: x=document.myform )

valid_cbox_field2=x.information.checked
valid_inp_field=x.reaction

if ( valid_cbox_field2 == true && valid_inp_field == false )

{
alert ("Please specify which information you wish to obtain.")
return false;
}

Why doesn't it return an alertbox when the checkbox is checked and the text field is empty?

Alternative Future

8:52 am on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Darkelve,

valid_inp_field=x.reaction.value;

if ( valid_cbox_field2 == true && valid_inp_field == "")

{
alert ("Please specify which information you wish to obtain.")
return false;
}

I think the above code should work ok for you...

HTH,

-george

Darkelve

8:57 am on Mar 3, 2005 (gmt 0)

10+ Year Member



Hi,

it doesn't... I left out the '[red]' text though. I guess that stands for 'redacted' or something?

Here is the full script:


function validate()
{
x=document.myform;

geldigenaam=x.naam.value;
if (geldigenaam == "")
{
alert("Vul aub de naam en voornaam van de contactpersoon in.")
return false
}

geldigadres=x.straat_nummer.value;
if (geldigadres == "")
{
alert("Vul aub het adres in.")
return false
}

geldigepostcode=x.postcode.value;
if (geldigepostcode == "")
{
alert("Vul aub de postcode in.")
return false
}

geldigegemeente=x.gemeente.value;
if (geldigegemeente == "")
{
alert("Vul aub de gemeente in.")
return false
}

geldigaanvinkveld1=x.abonnement_op1lijnkrant.checked
geldigaanvinkveld2=x.informatie_campagne.checked
if ( geldigaanvinkveld1 == false && geldigaanvinkveld2 == false )
{
alert("U dient minstens 1 van de 2 invulvelden aan te vinken om te weten te komen wat u wenst.");
return false
}

valid_inp_field=x.reaction.value;

if ( valid_cbox_field2 == true && valid_inp_field == "" )

{
alert ("Please specify which information you wish to obtain.")
return false;
}

Darkelve

9:01 am on Mar 3, 2005 (gmt 0)

10+ Year Member



Sorry, I had to edit some values cause my script is actually in Dutch, but I changed it for legibility.

It works fine now!

Thanks! :)

Alternative Future

9:21 am on Mar 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you have it working now? As I have a working version here also

-George

Darkelve

12:54 pm on Mar 3, 2005 (gmt 0)

10+ Year Member



Yes, it does work now. I translated my variable ('reaction') and forgot to translate it again when copying your script (for me variable is 'reactie').

I think I'm finally coming to grips with this Javascript stuff. I'm good at coding, but not programming! I really need exercises like these B)

Thanks again, you're very helpful.