473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How can I make a field become mandatory if a box is ticked.

57
Hi

I have a user enolment form which is users fill out to let IT know that they need setting up on 'x' systems. When the user put a cross in the 'I would like Email' checkbox I would like the form to make two other text field become mandatory. If they dont request email (which is the defult) the boxes will not be mandatory.

Thanks in advance.....
Aug 9 '07 #1
11 2418
acoder
16,027 Expert Mod 8TB
Show the form HTML markup.

You will need to check for the checked property of the checkbox object. If it's checked, check that the two now mandatory fields have something in them by checking their values. If they don't, alert an error message.
Aug 9 '07 #2
plumba
57
Hi all

This is probably a simple request which shows my level of novice of expertise in this..

I have a simple html form.
There is a box on the form which asks for the users Email address. This field is not mandatory...
BUT if a user clicks a Check Box which says 'Send me an email' then I would like a script to run which would the make the Email address field become mandatory..
It's got to be a simple - "if 'X' is ticked make 'Y' mandatory" but i cant figure it out...

Thanks in advance...
Aug 13 '07 #3
acoder
16,027 Expert Mod 8TB
Merged threads. Please do not double post.
Aug 13 '07 #4
plumba
57
Merged threads. Please do not double post.
Apologies - thought my second explaination was a bit clearer than the first! Even I coulnd't understand my first one! Still learning the ropes here...
Aug 13 '07 #5
epots9
1,351 Expert 1GB
Hi all

This is probably a simple request which shows my level of novice of expertise in this..

I have a simple html form.
There is a box on the form which asks for the users Email address. This field is not mandatory...
BUT if a user clicks a Check Box which says 'Send me an email' then I would like a script to run which would the make the Email address field become mandatory..
It's got to be a simple - "if 'X' is ticked make 'Y' mandatory" but i cant figure it out...

Thanks in advance...
please post your code so we can see the error.

thank you
Aug 13 '07 #6
plumba
57
please post your code so we can see the error.

thank you
I dont really have an error as such, I dont know where to begin :/

It just a really simple form which I want to add a checkbox/radio button which will call a script to make another field mandatory.

Could say, I'll be starting from scratch. If I could get just an example of how to get one box make another mandatory I should be able to take it from there. If sample code is needed I could knock summit up, but it's basically a blank sheet at the moment.

Cheers
Aug 13 '07 #7
epots9
1,351 Expert 1GB
code for checkbox
[html]
<form id="form1" name="form1">
<input type="text" id="email" name="email" />
<input type="checkbox" id="emailMe" name="emailMe" />
<input type="button" id="submit" name="submit" value="Submit" onclick="process();" />
</form>
[/html]

Expand|Select|Wrap|Line Numbers
  1. function process()
  2. {
  3. if(document.getElementById("emailMe").checked && document.getElementById("email").value == "")
  4. {
  5. //do something to tell the person to enter email
  6. }
  7. else
  8. {
  9. //do something when everything is ok
  10. }
  11. }
  12.  
u'll have to make some changes to make it work with your code.

good luck
Aug 13 '07 #8
plumba
57
code for checkbox
[html]
<form id="form1" name="form1">
<input type="text" id="email" name="email" />
<input type="checkbox" id="emailMe" name="emailMe" />
<input type="button" id="submit" name="submit" value="Submit" onclick="process();" />
</form>
[/html]

Expand|Select|Wrap|Line Numbers
  1. function process()
  2. {
  3. if(document.getElementById("emailMe").checked && document.getElementById("email").value == "")
  4. {
  5. //do something to tell the person to enter email
  6. }
  7. else
  8. {
  9. //do something when everything is ok
  10. }
  11. }
  12.  
u'll have to make some changes to make it work with your code.

good luck
Works a TREAT thank you..
Only one more question....
The If Then Else statement, I want to get it to alert then stop the submit going ahead. I've got:

function process()
{
if(document.getElementById("emailMe").checked && document.getElementById("Job_TitleW").value == "")
{
alert("To process Email enrolment you MUST provide the Welsh Job title. Please add then Click Submit")
return false
//do something to tell the person to enter email
}
else
{
//do something when everything is ok
}
}

But this doesn't seem to stop the code running...
Aug 13 '07 #9
epots9
1,351 Expert 1GB
based on the code i gave u...the form will never submit cuz i never designed it to. is your form submitting?
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("form1").submit();
  2.  
that will submit the form using javascript, must be placed in the else statement.

alittle reading about forms
Aug 13 '07 #10
plumba
57
based on the code i gave u...the form will never submit cuz i never designed it to. is your form submitting?
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("form1").submit();
  2.  
that will submit the form using javascript, must be placed in the else statement.

alittle reading about forms
Nope realised that, but I tied it into my form which submits this way:
<form method="post" action="mailto:email@work.com?subject....... etc etc

Just need the Java to say if this statement is not met then STOP the whole lot.
Aug 13 '07 #11
acoder
16,027 Expert Mod 8TB
Nope realised that, but I tied it into my form which submits this way:
<form method="post" action="mailto:email@work.com?subject....... etc etc

Just need the Java to say if this statement is not met then STOP the whole lot.
Call the validation function onsubmit:
Expand|Select|Wrap|Line Numbers
  1. onsubmit="return process();"
Aug 13 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MT | last post by:
Hi all, this sounds like an easy enough thing to do, but after spending 45 minutes searching google and various javascript sites I can't find out how to make a textfield (textbox or whatever you...
12
by: Adam Lipscombe | last post by:
Folks, I need to get the contents of a form attribute. In Read/Write mode this is field, so I can use getElementById("name").value In ReadOnly this is just plain text, so I can use...
5
by: jaking01 | last post by:
Hi. I am currently working on a form that I need to prevent changes to after data has been input. There are a series of tick boxes on the form, and when each one of these is ticked it spawns...
23
Cyberdyne
by: Cyberdyne | last post by:
I have two fields Field1 - has options Yes or No Field2 - has three options to chose from First: What do I need to do to make Field1 be No and Field2 not selectable by default? Second: If...
4
by: andrewbda | last post by:
I have tickbox on a form, and I would like to use a date field as the control source. i.e. I would like to have it display as ticked when a date exists in the field, and vice versa. Also,...
4
by: AR123 | last post by:
In the field for employee contribution I want to have GBP and make sure that this prints out in the results email. How can I set it up so that the GBP display next to the value in the results email?...
1
by: AR123 | last post by:
The mandatory form field that is not working is the TYPE OF ILLUSTRATION. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Proforma</title> <meta...
1
by: mlshoats | last post by:
Hello I am trying to make a log to track questions i answer on little hardware problems. I got most of it working but I am very new to programming anything and just started picking up things. I am...
3
by: Gaurav Jhamb | last post by:
I have 2-3 forms.On my first form when i serach to put any word it sends results and after that i click on new button in my search i got different organisation names. on the secong form i passed the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.