简体   繁体   English

在cognos中使用javascript进行日期验证

[英]validation of date using javascript in cognos

  1. When the users select the To Period as < From Period then an error message should be thrown to select a period >= From Period 当用户选择“到期间”为“ <从期间”时,将引发错误消息以选择一个期间> =“从期间”

  2. When the user selects more than 12 months then a warning message should be displayed as "The report will take more time to display the dashboard". 当用户选择超过12个月时,警告消息应显示为“报告将花费更多时间来显示仪表板”。

The from prompt and to prompt are drop down and the use value is integer (eg. jan 2013 =1 ,feb 2013=2) 从提示和到提示下拉列表,并且使用值是整数(例如,2013年1月= 1,2013年2月= 2)

I need a java script to validate and display the error message on selection of the dates. 我需要一个Java脚本来验证并在选择日期时显示错误消息。 Before submitting the report. 提交报告之前。

Thanks in Advance 提前致谢

If I understand correctly you want to validate that the from date is before the to date. 如果我理解正确,则需要验证起始日期是否在起始日期之前。

I do not know if you are using plain JS or jQuery but for whichever you use retrieve the values from your inputs in the usual way. 我不知道您使用的是普通的JS还是jQuery,但是无论使用哪种方法,都可以以通常的方式从输入中检索值。

<script type="text/javascript">
function checkDates() {
  var to = document.getElementsByName('to')[0].value;
  var from = document.getElementsByName('from')[0].value;

  if(to < from) {
    alert('To date must be after from date!');
  } 
}
</script>

<select name="from" onchange="checkDates()">
  <option value="1">JAN</option>
  <option value="2">FEB</option> 
  ...
</select>

<select name="to" onchange="checkDates()">
  <option value="1">JAN</option>
  <option value="2">FEB</option> 
  ...
</select>

If we put eshortie's code into a validation function and we define the submit event of the form, then it should work. 如果我们将eshortie的代码放入验证函数中,并且定义了表单的Submit事件,则它应该起作用。 Example: 例:

$(function(){
    $("#myform").submit(function(){
        var validactionalResult = validation();
        if (validationalResult !== true) {
            alert(validationalResult);
        } else {
            $.ajax({
            type: "POST",
            url: "yoururl",
            data: $(this).serialize(),
            success: function(response) {
                    if (response === "success") {
                        //do something
                    } else {
                        alert(response);
                    }
                },
            error: function() {
                    alert("Unsuccessful connection attempt");
                }
            });
        }
        return false;
    });
});

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

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