Allow year only after 1900

Posted by: samvarth123 on 1 July 2020, 3:33 am EST

  • Posted 1 July 2020, 3:33 am EST

    Hi Team !

    Is it possible when we type a year something like 0205 or 1202 it should get back to previous year which was there or in case its the first time they are doing we should make the complete thing null.

    So basically we should allow only years after 1900.

    We are using angular 7 with wijmo flexgrid.

    Thanks,

    Siva

  • Posted 1 July 2020, 3:35 am EST

    <wj-flex-grid-column [header]=“‘Start Date’” [binding]=“‘startDate’”>

    Using similar to this in html

  • Posted 1 July 2020, 5:55 pm EST

    Hi Siva,

    Simply handle the cellEditEnding event of FlexGrid and if the current value entered is less than 1900, cancel the event.

    Please refer to the sample link below for reference:

    https://stackblitz.com/edit/angular-g4broy

    Regards,

    Ashwin

  • Posted 1 July 2020, 6:03 pm EST

    Hi Ashwin !

    But date is like 08/06/1901 not just the year.Rest of the code sample in html you wrote is as it is except for the format

  • Posted 1 July 2020, 6:04 pm EST

    Could you please let me know how to do in the above case

  • Posted 1 July 2020, 6:26 pm EST

    In case of a full date column, just parse the entered value as a Date object and check whether the year is greater than 1900. Refer to the sample below:

    https://stackblitz.com/edit/angular-zbgv88

    ~regards

  • Posted 3 July 2020, 2:34 am EST

    Ashwin ! I tried with that but the problem is s.activeEditor.value is always giving the old value .But in sample you gave it is getting new value.

    So I tried with init method

    s.itemFormatter = (panel, r, c, cell) => {

    if (r > 0 && panel.rows[r].dataItem) {

    const item = panel.rows[r].dataItem;

    }

    s.formatItem.addHandler((s, e) => {

    const col = s.columns[e.col];

    if (e.panel.cellType !== wjcGrid.CellType.Cell) {

    return;

    }

    if (s.columns[e.col].binding === ‘startDate’) {

    let dt = s.getCellData(e.row, e.col);

    if (dt) {

    dt = new Date(dt);

    //const date = wjcCore.Globalize.parseDate(dt.toISOString(), col.format); // the date entered should be in correct format

    // e.cancel = (date && date.getFullYear() <= 1900); // cancel the event is less than 1900

    if(!(dt.getFullYear() >= 1900)){

    this.cancelDatechange = true;

    }

    e.cancel = !(dt.getFullYear() >= 1900);

    }

    But problem is e.cancel is true here but invalid date is still there and it didn’t stop.

    Any pointers ?

  • Posted 5 July 2020, 6:13 pm EST

    Hi Siva,

    Can you let me know why are you using formatItem event? This event is used to format the cells and not to check whether the value entered is valid or not. Also, in formatItem event, e.cancel have no value because the cancel property does not exists in the FormatItemEventArgs (which are provided as the event args of formatItem).

    You should use the cellEditEnding event as I suggested in the previous example. I also found another way to check this. Instead of using cellEditEnding, you may use the getError property of the CollectionView to check whether the date entered is valid. Please refer to the sample below:

    https://stackblitz.com/edit/angular-xsze9r

    Using getError will also display an error if the date entered is less than 1900.

    ~regards

  • Posted 6 July 2020, 3:19 am EST

    It works for the first time.Later when I set a date in 2020 it still shows the error when I try to debug it was always showing old date which is below 1900.Its working fine in your example when a valid date is given but not in my sample

  • Posted 6 July 2020, 4:48 pm EST

    Hi Siva,

    Can you please modify the sample above so that it replicates your issue? Also, it seems that you are using formatItem event to check the value of the cell. I would recommend you use the cellEditEnding event.

    ~regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels