我有这个XHTML代码。在此代码中,Javascript验证不会运行:
<h:form id="form1" >
<h:outputText for="activity_start_date" value="start date: " />
<p:calendar id="activity_start_date" navigator="true" mindate="01/01/1950" yearRange="c-50:c+10" value="#{loginBean.activity_start_date}" onblur="checkdate(this)" widgetVar="from"
pattern="dd/MM/yyyy" required="true" immediate="true" requiredMessage="Enter start date" />
<h:message for="activity_start_date" style="color:red"/>
<h:outputText for="activity_end_date" value="end date: " />
<p:calendar id="activity_end_date" navigator="true" mindate="01/01/1950" yearRange="c-50:c+10" pattern="dd/MM/yyyy" onblur="checkdate(this)" widgetVar="to"
value="#{loginBean.activity_end_date}" required="true" immediate="true" requiredMessage="Enter end date" />
<h:message for="activity_end_date" style="color:red"/>
这是我的Javascript:
<script type="text/javascript">
function CompareDates()
{
var str1 = document.getElementById("form1\:activity_start_date").value;
var str2 = document.getElementById("form1\:activity_end_date").value;
alert("hai");
var dt1 = parseInt(str1.substring(0,2),10);
var mon1 = parseInt(str1.substring(3,5),10);
var yr1 = parseInt(str1.substring(6,10),10);
var dt2 = parseInt(str2.substring(0,2),10);
var mon2 = parseInt(str2.substring(3,5),10);
var yr2 = parseInt(str2.substring(6,10),10);
var date1 = new Date(yr1, mon1, dt1);
var date2 = new Date(yr2, mon2, dt2);
if(date2 > date1)
{
alert("To date cannot be greater than from date");
return false;
}
else
{
alert("Submitting ...");
document.form1.submit();
}
}
</script>
我正在使用 primefaces3.0 + jsf 。当我按下提交按钮时,它会转到脚本部分,但它不会执行脚本体。你能给我一个解决方案吗?
答案 0 :(得分:2)
如果您使用内置的JSF验证函数,您将获得更多运气并最终获得更清晰的代码:
http://download.oracle.com/javaee/6/tutorial/doc/bnatn.html#bnatr
在这种情况下,您正在相互验证字段,以下链接解释了如何在jsf中很好地执行此操作:
http://balusc.blogspot.com/2007/12/validator-for-multiple-fields.html