简体   繁体   中英

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

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

I need a java script to validate and display the error message on selection of the dates. 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.

<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. 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;
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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