Solved

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

Posted on 2008-11-02
9
240 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
  • 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
 
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now