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

x
?
Solved

Coldfusion Form Validation - Require Only One of Many Fields

Posted on 2008-10-16
6
Medium Priority
?
307 Views
Last Modified: 2013-12-16
I have a form that asks for workphone, cellphone, homephone and email.  I want to validate the form so that only one of the 4 fields is required.  I need to use cffom because I am also passing variables to some of the form values.  How can I show a message box stating that a "Phone number or email is required" if all of these fields are left blank?
0
Comment
Question by:lonnyo
[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
  • 3
  • 2
6 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 22731173
something like this should work
<script type="text/javascript">
var ok
ok=0
function checkCom()
{
	if(document.getElementById('f1').value=='')
	{ok=ok+0}else{ok=ok+1}
	
	if(document.getElementById('f2').value=='')
	{ok=ok+0}else{ok=ok+1}
	if(document.getElementById('f3').value=='')
	{ok=ok+0}else{ok=ok+1}
	if(ok==0)
	{
		alert("Phone number or email is required")
		return false}
	else
	{return true}
alert(ok)
}
</script>
<cfform name="test" method="post" onsubmit="checkCom();">
<cfinput id="f1" name="f1" type="text">
<cfinput name="f2" id="f2" type="text">
<cfinput name="f3" id="f3" type="text">
<cfinput id="sub" type="submit" name="sub">
</cfform>

Open in new window

0
 

Author Comment

by:lonnyo
ID: 22731495
The problem with adding javascript to validate the form is that the form itself is also creating a javascript script and they seem to cancel each other out.  I originally had javascript working on a regular html form and then decided I needed to use <cfform> because I need to populate values in some of the input fields.  I guess the next question is can I somehow add/edit the javascript that is created by coldfusion or use more than one script on the same cfform?
0
 
LVL 36

Expert Comment

by:SidFishes
ID: 22731661
as you've found, mixing js and cfform js can be problematic it's best to stick to one or the other. In some cases you can get it to work ...others no.. depends on what you're doing

"because I need to populate values in some of the input fields."

not sure what you mean by this...

you can populate a standard html form with cf variable just as easily

<input type="text" name="foo" value="#myquery.myvalue#">
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:lonnyo
ID: 22732288
I forgot I could do that as long as I put the input tag inside a cfoutput tag.  Wow do I feel dumb.  Thanks for the help.
0
 
LVL 36

Accepted Solution

by:
SidFishes earned 2000 total points
ID: 22732465
please remember to close the q
0
 

Expert Comment

by:Bward323
ID: 23665518
This code helped me but there was one error in line 22:

<cfform name="test" method="post" onsubmit="checkCom();">
  SHOULD READ
<cfform name="test" method="post" onsubmit="return checkCom();">
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

When deciding to adopt any help desk solutions many factors should be explored before taking decisions. This will change from business to another but in general there are some kind of rule of thumb. Here are some quick tips: Do we need only ticket…
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

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