简体   繁体   English

jsp中的bootstrap-datetimepicker验证

[英]bootstrap-datetimepicker validations in the jsp

I am using spring-mvc and bootstrap in my web application. 我在Web应用程序中使用spring-mvc和bootstrap。 In on of the pages I have 2 fields which are for the user to enter startdate-time and end date-time, this I picked up from bootstrap-datetimepicker.js https://github.com/smalot/bootstrap-datetimepicker 在页面的其中一个中,我有2个字段供用户输入开始日期时间和结束日期时间,这是我从bootstrap-datetimepicker.js https://github.com/smalot/bootstrap-datetimepicker拾取的

This works fine, but now I need to include validations, that startdate-time is always greater than enddate-time, I am also using jquery.validate.min.js for form validations, but I am not able to compare the 2 timestamps, I tried the below code 效果很好,但是现在我需要包含验证,开始日期时间总是大于结束日期时间,我也使用jquery.validate.min.js进行表单验证,但是我无法比较两个时间戳,我尝试了以下代码

$.validator.addMethod("budgetgreaterThan", 
          function(value, element, params) {
      alert(value);
      value=value.slice(0,-6);


        if (!/Invalid|NaN/.test(new Date(value))) {
            return new Date(value) >= new Date($(params).val());
        }
        return isNaN(value) && isNaN($(params).val()) 
            || (Number(value) > Number($(params).val())); 
    },'Must be greater than {0}.');

rules:
{
  budgetgreaterThan: '#startdate' 
}

messages:
{
    budgetgreaterThan:"Delivery date and Collect date should be proper"
}

the timestamp value here is in this format "YYYY-MM-DD hhhh:mm:ss" 此处的时间戳值格式为"YYYY-MM-DD hhhh:mm:ss"

Can anyone please tell me how do I achieve this? 谁能告诉我如何做到这一点?

I solved this issue by making an ajax call and verifying at the backend, I avoided $.validator.addMethod() and instead used a onblur="myFunction()" 我通过进行ajax调用并在后端进行验证来解决此问题,我避免了$.validator.addMethod() ,而是使用了onblur="myFunction()"

<input type="text" id="enddate" name="enddate" onblur="myFunction()">

<script>
function myFunction()
{

  var sdate = $('#startdate').val();
  var edate = $('#enddate').val();

  var totaldate = sdate+"/"+edate;

   $.ajax({
     url: '${pageContext.servletContext.contextPath}/developer/createinstance/TimeCheck.prm',
     type: 'get',
     data: "totaldate="+totaldate,
     success: function (data1) {
       if(data1 == -1)
       {     
         bootbox.alert("enddate cannot be lesser than start date");
         $('#startdate').val("");
         $('#enddate').val("");
       }
       if(data1 == 0)
       {
         bootbox.alert("date cannot be same");
         $('#startdate').val("");
         $('#enddate').val("");
       }
     }
   });
}
</script>

but I am sure there is a better way to implement this, if anyone can give a better solution it will be great. 但是我相信有更好的方法来实现这一点,如果有人可以给出更好的解决方案,那将是很好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM