Validation of date using Javascript in cognos

2.6k Views Asked by At
  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".

The from prompt and to prompt are drop down and the use value is integer (e.g. jan 2013 =1 ,feb 2013=2)

I need a Javascript to validate and display the error message on selection of the dates. Before submitting the report.

2

There are 2 best solutions below

2
On

If we put eshortie's code into a validation function and we define the submit event of the form, then it should work. 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;
    });
});
5
On

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.

<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>