Solved

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

Posted on 2008-11-02
9
271 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 150 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

739 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