如何验证 primefaces 日期
我有这个 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。当我按下提交按钮时,它会转到脚本部分,但不会执行脚本主体。您能给我一个解决方案吗?
I have this XHTML code. In this code, the Javascript validation doesnt run:
<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"/>
This is my 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>
I am using primefaces3.0+jsf. When I press the submit button it goes to the script section, but it doesn't execute body of script. Could you please give me a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用内置的 JSF 验证函数,您将会有更多的运气并最终得到更干净的代码:
http://download.oracle.com/javaee/6/tutorial/doc/bnatn.html#bnatr
在这种情况下,您要相互验证字段,以下链接解释了如何做到这一点在jsf中很好:
http://balusc.blogspot.com/ 2007/12/validator-for-multiple-fields.html
You'll have much more luck and end up with cleaner code if you use the built in JSF validation functions:
http://download.oracle.com/javaee/6/tutorial/doc/bnatn.html#bnatr
In this case you're validating fields against each other, the following link explains how to do this in jsf very well:
http://balusc.blogspot.com/2007/12/validator-for-multiple-fields.html