Search code examples
javascripthtmlformscoldfusioncoldfusion-9

Form validation with javascript in coldfusion


I am trying to validate two fields. One is open time and the other one is close time. My validation should check if the close time has a value anything else than "00" my open time should have a value greater than "00" as well.

My question is, what am I doing wrong? I know I must have an error in my function.

This is the validation that I wrote so far:

<script type="text/javascript">

function hoursFunction(formObject, formField, fieldValue)
{  
  var closeHours = document.getElementById('closeHours#CountVar#');
  var openHours = document.getElementById('openHours#CountVar#');

  if(closeHours != "00" && openHours == "00")
  {
    sfm_show_error_msg('Enter a valid opening time');
  }
}

And this a sample of my form

<form id="hoursForm" action="physician_businessHours.cfm?docID=<cfoutput>#docid#</cfoutput>" method="post" onsubmit="hoursFunction();">
            <input type="hidden" name="postitnow" value="yes">


                    <table border="1">

                    <tr>
                        <th>Day</th><th>Open Time</th><th>Close Time</th>
                    </tr>


                    <cfloop from="1" to="7" index="CountVar">

                    <cfset dayFound= 0>
                    <tr>
                        <td><cfif CountVar eq 1>Mon<cfelseif CountVar eq 2>Tues<cfelseif CountVar eq 3>Wednes<cfelseif CountVar eq 4>Thurs<cfelseif CountVar eq 5>Fri<cfelseif CountVar eq 6>Satur<cfelseif CountVar eq 7>Sun</cfif>day</td>

                        <cfoutput>

                        <td>   
                        <select id="openHours#CountVar#" name="openHours#CountVar#">
                         <cfloop query="doctorHours">
                        <cfloop from="00" to="23" index="OpenHours">  
                        <option  value="#openHours#"<cfif TimeFormat(doctorHours.openTime,'HH') EQ OpenHours AND CountVar EQ doctorHours.day > selected="selected"</cfif>>#OpenHours#</option>
                        </cfloop>
                        </cfloop>
                        </select>
                         </td>

     <td>     
                        <select id="closeHours#CountVar#" name="closeHours#CountVar#">
                        <cfloop query="doctorHours">
                        <cfloop from="00" to="23" index="closeHours">  
                        <option value="#closeHours#"
                        <cfif TimeFormat(doctorHours.closeTime,'HH') EQ closeHours AND CountVar EQ doctorHours.day  > selected="selected"</cfif>>#closeHours#</option>
                        </cfloop>
                        </cfloop>
                        </select>
                        </td>
                         </tr>

      <input type="hidden" name="Validate" onValidate="hoursFunction" message="You must select an opening hour">

     <input type="submit" value="Update" id="Submit">
                    '

Solution

  • This is the code that I wrote and it works fine so far:

    function hoursFunction()
    {  
    
        var i = 0;
        var openHour;
        var closeHour;
    
        for(i=1;i<8;i++)
        {
         openHour = document.getElementById("openHours" + i).value;
         closeHour= document.getElementById("closeHours" + i).value;
    
        if(openHour > closeHour)
        {   
            document.getElementById('error').innerHTML= "Error at " + i;  
            return false; 
        }
        document.getElementById('error').innerHTML= "No Error occured";  
        return true;
        }
    }
    

    My issue now is that when I submit the form it seems that the form is doing a refresh and is deleting all of my data... Any thoughts on that?