在 coldfusion 中使用 javascript 进行表单验证

Form validation with javascript in coldfusion

本文关键字:表单 验证 javascript coldfusion      更新时间:2023-09-26

我正在尝试验证两个字段。一个是开放时间,另一个是关闭时间。我的验证应该检查关闭时间的值是否为"00"以外的任何值,我的打开时间也应该具有大于"00"的值。

我的问题是,我做错了什么?我知道我的函数一定有错误。

这是我到目前为止写的验证:

<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');
  }
}

这是我的表单示例

<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">
                    '

您需要从元素中获取值:

  var closeHours = document.getElementById('closeHours#CountVar#').value;
  var openHours = document.getElementById('openHours#CountVar#').value;

我明白了。您需要获取正确的元素 ID:

for (var i = 0; i < 7; i++) {
    var closeHours = document.getElementById('closeHours' + i).value;
    var openHours = document.getElementById('openHours' + i).value;
    if (closeHours != "00" && openHours == "00") {
      sfm_show_error_msg('Enter a valid opening time');
    }
}

你的 CountVar 从 1 开始,而不是 0,所以你的 for 循环应该是相同的。另外,为了防止表单提交,如果表单未验证,您的 hoursFunction() 应返回 false。此外,您的 hoursFunction() 需要 3 个参数,但您没有向它传递任何参数 - 在这种情况下,这些参数不是必需的,因此只需从函数定义中删除它们即可。尝试以下功能:

function hoursFunction() {
  for (var i = 1; i <= 7; i++) {
    var closeHours = document.getElementById('closeHours' + i).value;
    var openHours = document.getElementById('openHours' + i).value;
    if (closeHours != "00" && openHours == "00") {
      sfm_show_error_msg('Enter a valid opening time');
      return false;
    }
    return true;
  }
}

这是我编写的代码,到目前为止工作正常:

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;
    }
}

我现在的问题是,当我提交表单时,表单似乎正在刷新并删除我的所有数据......对此有什么想法吗?