如何验证 primefaces 日期

发布于 2024-12-13 02:11:57 字数 1917 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2024-12-20 02:11:57

如果您使用内置的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文