I use this when I have several fields to validate. This is from a project I did, so make sure to change the items to fit your form.

<script language="_javascript_">
/*
Required field(s) validation- By Bruce Sorge

*/

function formCheck(formobj){
//1) Enter name of mandatory fields
var fieldRequired = Array("ID_Type", "IDNumber", "Gender");
//2) Enter field description to appear in the dialog box
var fieldDescription = Array("ID Type", "ID Number", "Gender");
//3) Enter dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
  var obj = formobj.elements[fieldRequired[i]];
  if (obj){
   switch(obj.type){
   case "select-one":
    if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
     alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
   case "select-multiple":
    if (obj.selectedIndex == -1){
     alertMsg += " - " + fieldDescription[i] + "\n";
    }
    break;
   default:
    if (obj.value == "" || obj.value == null){
     alertMsg += " - " + fieldDescription[i] + "\n";
    }
   }
  }
}

if (alertMsg.length == l_Msg){
  return true;
}else{
  alert(alertMsg);
  return false;
}
}
//  End -->
</script>

<form name="Step_3" action="" method="post" formCheck(this);">
<tr>
            <td width="20" class="FormFieldTDGrey">Type of ID</td>
            <td class="FormFieldTD"><select name="ID_Type" size="1">
                      <option></option>
                      <option value="Drivers License">Drivers License</option>
                      <option value="State Issued ID">State Issued ID</option>
                      <option value="Social Security Card">Social Security Card</option>
                      <option value="Passport">Passport</option>
                      <option value="Military ID">Military ID</option>
                      <option value="School ID">School ID</option>
                    </select></td>
          </tr>
          <tr>
            <td width="20" class="FormFieldTDGrey">Last 4 of ID</td>
            <td class="FormFieldTD"><input type="text" name="IDNumber" maxlength="4" size="4">
            (SSN, Drivers License, etc..)</td>
          </tr>
          <tr>
            <td width="20" class="FormFieldTDGrey">Date of Birth</td>
            <td class="FormFieldTD"><SELECT name="monthform" size="1">
      <CFLOOP index="month_number" from=1 to=12>
      <CFOUTPUT><OPTION value="#month_number#">#MonthAsString(month_number)#</CFOUTPUT>
      </CFLOOP>
      </SELECT>
      <select name="dayform" size="1">
      <cfloop index="day_number" from="1" to="31">
      <cfoutput><option value="#day_number#">#day_number#</option></cfoutput>
      </cfloop>
      </select>
      <SELECT name="yearform" size="1">
      <CFLOOP index="year_number" from=1920 to=2002>
      <CFOUTPUT><OPTION value="#year_number#">#year_number#</CFOUTPUT>
      </CFLOOP>
      </SELECT> </td>
          </tr>
          <tr>
            <td width="20" class="FormFieldTDGrey">Gender</td>
            <td class="FormFieldTD"><select name="Gender">
     <option></option>
                          <option value="Male">Male </option>
                          <option value="Female">Female </option>
                        </select></td>
          </tr>
<input type="submit" name="Submit" value="ENTER" class="submitButton">
</form>

Hope this helps.

Bruce
  ----- Original Message -----
  From: Stuart Kidd
  To: CF-Talk
  Sent: Saturday, February 07, 2004 6:12 PM
  Subject: RE: Please make selection ----->

  Thanks for that Bruce.  I'm working on the validation right now. I got
  it to work no probs when there was just one box but what about if there
  are a few to validate?

  I'm not too good on the _javascript_.

  <script language="_javascript_">
  //Need to check and see that if they selected something other than an
  empty ("") field
  function checkFormSelect() {
     Var= AddProperty.PropertyDataNumberofBedrooms.selectedIndex;
       if (AddProperty.PropertyDataNumberofBedrooms.options[Var].value ==
  "")  {
        alert("You must select the number of bedrooms.");
        return false;
    }
    else if
    {
  function checkFormSelect() {  
     Var= AddProperty.PropertyDataNumberofBathrooms.selectedIndex;
       if (AddProperty.PropertyDataNumberofBathrooms.options[Var].value ==
  "")  {
        alert("You must select the number of bathrooms.");
        return false;
    }
    else  if
    {   
  function checkFormSelect() {  
     Var= AddProperty.PropertyDataHouseType.selectedIndex;
       if (AddProperty.PropertyDataHouseType.options[Var].value == "")  {
        alert("You must select the type of house.");
        return false;
    }
    else
    {     
     return True;   
    }
    }

  </script>

  Is it right to say it should be 'else if' ?  Or is that just CF??

  Cheers,

  Stuart

  -----Original Message-----
  From: Bruce Sorge [mailto:[EMAIL PROTECTED]
  Sent: 07 February 2004 22:29
  To: CF-Talk
  Subject: Re: Please make selection ----->

  For select lists, I use this:

  <script language="_javascript_">
  //Need to check and see that if they selected something other than an
  empty ("") field
  function checkFormSelect() {
     Var= FormName.FieldName.selectedIndex;
       if (FormName.FieldName.options[Var].value == "")  {
        alert("Enter alert message here");
        return false;
    }
    else
    {
     return True;   
    }
    }

  </script>
  <body>

  <form name="FormName" method="get" action="" >   checkFormSelect()">
  <Select name="FieldName">
    <option value="">Please Select an Item
    <option value="ThisItem">This Item
  </SELECT>
  <BR>
  <input type="Submit">
  </form>

  In your Form tag, just use the onSubmit variable. If the field is
  selected with something other than an empty variable, it will submit,
  otherwise it will return false and show the user the error message. You
  can also add a focus attribute to the script to focus them onto the
  select list.

  For the other part, you can use _javascript_ and a div tag:

  <script>
  //This script will trigger three text fields if the caller has called in
  before.
  function hide ( name ) {
  NS4 = (document.layers) ? 1 : 0;
  IE4 = (document.all) ? 1 : 0;
  W3C = (document.getElementById) ? 1 : 0;        
  if (W3C) {
    document.getElementById(name).style.visibility = "hidden";
  } else if (NS4) {
    document.layers[name].visibility = "hidden";
  } else {
    document.all[name].style.visibility = "hidden";
  }
  }
  function showlayer (name ) {
  var currentX,           //mouse position on X axis
  currentY,               //mouse position on X axis
  x,              //layer target position on X axis
  y,              //layer target position on Y axis
  docWidth,               //width of current frame
  docHeight,      //height of current frame
  layerWidth,     //width of popup layer
  layerHeight;    //height of popup layer
  var ele;                //points to the popup element

  NS4 = (document.layers) ? 1 : 0;
  IE4 = (document.all) ? 1 : 0;
  W3C = (document.getElementById) ? 1 : 0;

  // First let's initialize our variables
  if ( W3C ) {
    //alert("w3c");
    ele = document.getElementById(name);
    docWidth = document.width;
    docHeight = document.height;
    layerWidth = ele.style.width;
    layerHeight = ele.style.height;

  } else if ( NS4 ) {
    ele = document.layers[name];
    docWidth = document.width;
    docHeight = document.height;
    layerWidth = ele.clip.width;
    layerHeight = ele.clip.height;

  } else {        // meant for IE4
    ele = window.opener.document.all[name];
    docHeight = document.body.offsetHeight;
    docWidth = document.body.offsetWidth;
    layerWidth = 200;
    layerHeight = ele.offsetHeight;
  }

  if ( NS4 ) {
    ele.visibility = "show";
  } else {  // IE4 & W3C
    ele.style.visibility = "visible";
  }
  }

  </script>

  <td class="td" align="left">
      <input type= radio name = Action value = NewCustomer checked
  > No
      <input type= radio name = Action value = CurrentCustomer
  > Yes
      <div style="visibility:hidden" id="divName">
      Enter User Name&nbsp;<input type= text name=LoginID size=10
  target=_parent><BR>
      Enter First Name&nbsp;<input type="text" name="Fname" size="10"
  target=_parent><BR>
      Enter Last Name&nbsp;<input type="text" name="Lname" size="10"
  target=_parent>
      </div>  
     </td>

  Although this is for radio buttons and text fields, you can modify it to
  use select lists.

  Hope this helps,

  Bruce
    ----- Original Message -----
    From: Stuart Kidd
    To: CF-Talk
    Sent: Saturday, February 07, 2004 3:44 PM
    Subject: Please make selection ----->

    Hi guys,

    With usual text boxes in forms, CF can make sure the user has filled
    them in but how can I get it to make sure with select boxes the user
    doesn't leave the default "Please make selection----->" part selected
    (which will of course bring up an error in the action page).  Isn't
    there a way I can fix errors on the initial page?

    Also, a little off topic, but is there a way with Dreamweaver to
  display
    certain select boxes only if others are selected.  For instance if
  they
    select "For Rent" the next select box for price will be a list of
    "Weekly Price" and "Monthly Price" and then followed by a text box for
    the user to insert price.  But had they chose "For Sale" then the next
    select box would be just the price text box.

    Thanks,

    Stuart (Saturday night at home)
    _____
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to