Solved

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

Posted on 2008-11-02
9
232 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

706 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

18 Experts available now in Live!

Get 1:1 Help Now