Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Validateion text needed - one of two fields on form must be filled out

Posted on 2008-11-02
9
Medium Priority
?
276 Views
Last Modified: 2008-11-02
Hi experts!  I have a form with two checkboxes, each with their own field.  They are "Male" and "Female."  I need to ensure that before closing the form that one of the fields has been checked.  Any ideas?
0
Comment
Question by:slamboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Expert Comment

by:Rodger
ID: 22861495
You would need to but some code on the onClose Event of the form.  I would also remove the close button on the form.  Is this the only form or is it part of larger form?
As a note to making sure you have the structure correct.  Is male and female their own fields or do you have one field for gender?  Also you might want to use radio button in a group that way the user can only select one.  A checkbox implies that the user can make more then one selection.
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 22861512
Hey there mate!

I provided a sample, javascript will be used here to validate the script.  You can try the script out. I used radio buttons, as I suppose I think you ment that? Checkboxes allow multiple selections, and I don't think they can be both male and female :)

Some small things:

When declaring, use 'document.myformname.radiobuttonname[x] ... In my example you will see myformname is 'myform', radiobuttonname is 'Gender' and [x] will be 0 and 1, because we have TWO optionbuttons. If we had more, we would continue this, [2],[3] etc.

If you want to validate more then just radio buttons, check out this:

http://www.yourhtmlsource.com/javascript/formvalidation.html

Hope this helps!
<form name="myform" method="post" onsubmit="return checkform(this);">
<input type="radio" name="Gender" value="Male"> Male<br>
<input type="radio" name="Gender" value="Female"> Female<br>
<input type="submit" value="GO">
</form>
 
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
  // ** START **
	if (!document.myform.Gender[0].checked &&
	!document.myform.Gender[1].checked) {
			alert( "Please enter gender!" );
		return false;
	}
 
  // ** END **
  return true ;
}
//-->
</script>

Open in new window

0
 

Author Comment

by:slamboy
ID: 22861539
RodgerDjr,

I am looking for a code example here.  Yes, they are separate fields.  I should have probably used radio buttons but I'm too far down the road to change things now (lots & lots of queries).  I have this situation with forms and subforms.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Valleriani
ID: 22861617
Here then, if you want for checkboxes, don't think its a good idea, but I added a extra check to make sure that both are not checked as well.
<form name="myform" method="post" onsubmit="return checkform(this);">
<INPUT TYPE=CHECKBOX NAME="Male">Male<P>
<INPUT TYPE=CHECKBOX NAME="Female">Female<P>
<input type="submit" value="GO">
</form>
 
<script language="JavaScript" type="text/javascript">
<!--
function checkform ( form )
{
  // ** START **
	if (!document.myform.Male.checked && !document.myform.Female.checked) {
		 alert( "Please enter gender!" );
		return false;
	}
	if (document.myform.Male.checked && document.myform.Female.checked) {
		 alert( "You cannot select both genders!" );
		return false;
	}
  // ** END **
  return true ;
}
//-->
</script>

Open in new window

0
 
LVL 4

Expert Comment

by:Rodger
ID: 22861698
i still would use radio buttons, they will not harm the queries.
You would just need to use an if statement.
If isNull(me.checkbox1) or isNull(me.checkbox2) then
     msgbox('You must select male or female", vbOKOnly, "Your Title here")
     docmd.CancelEvent
else
end if
0
 
LVL 4

Expert Comment

by:Rodger
ID: 22861737
Vallerini, this is an MS Access fourm not Javascript

:)
0
 
LVL 7

Expert Comment

by:Valleriani
ID: 22861750
Ack! Sorry! I totally must of clicked the wrong area, my bad.
0
 

Author Comment

by:slamboy
ID: 22861796
RodgerDjr, this is exactly what I was looking for.  However I forgot I have a macro that runs when the form closes.  Any ideas on how I could still run this check?
0
 
LVL 4

Accepted Solution

by:
Rodger earned 600 total points
ID: 22861813
Have the macro run the code.  I do not do much with Macors just VBA Code.  I am pretty sure there is a command RunCode.  Or just put that code on the obClose event of the form.  Macros and code can work together.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question