0

I'm creating a registration form with ColdFusion. One of the requirement is for user to select a value from a drop down. When one of the option is selected, the next textbox field need to be fill in so this field becomes required field. If user does not select any option from a drop down then this texfield can be left blank. I'm not good with Javascript, is there a way to get some free sample? Here is my form fields:

<cfselect name="OtherContact" class="inputSelect">
  <option value="">--- Select other contact ---</option> 
  <option value="HomePhone">Home Phone</option>                 
  <option value="HomeFax">Home Fax</option>             
  <option value="HomeEmail">Home Email</option>             
</cfselect>

<cfinput type="text" name="OtherContactId" value="#Form.OtherContactId#" class="inputText">
Jeromy French
  • 11,812
  • 19
  • 76
  • 129
user2127860
  • 69
  • 1
  • 6

1 Answers1

0

What you need to do is before the form is submitted to see if the dropdownlist selected index is different from 0, if it is, then the text of your textbox must be different from an Empty String. This is an example:

// this is the javascript function that will make sure your criteria is found, if it does, it will return true, false otherwise
function validateSubmit(){
 var OtherContact= document.getElementById('<%=OtherContact.ClientID%>')
 if (OtherContact.selectedIndex !== 0){
    if (document.getElementById('<%=OtherContactId.ClientID%>').value === ""){
       return false;
    }
  }
return true;
}

So, before you submit (or do whatever you want to do after the validation), you can do this:

// function that submits 
function submit(){
if (validateSubmit()){
// your code in case validation is passed.
   }
else{
// your code in case validation is not passed.
   }
}

Good luck.

Felipe Gavilán
  • 393
  • 2
  • 10
  • I have to put OnSubmit on my submit button tag: ? and what do you mean by // your code in case validation is passed and // your code in case validation is not passed. – user2127860 Mar 03 '13 at 01:55
  • I don't know much about coldfusion, but in asp.net I would simply put: Sorry for not explaining the "//", in javascript that's the way you add comments to your code. So the comments indicate what you may want to do in case the validation is passed. For example, usually, when validation is not passed, you can use an alert("you must fill the textbox") to let the user know that the validation wasn't passed. – Felipe Gavilán Mar 03 '13 at 14:16