Category / Section
How to enter only numeric values in a JavaScript Spreadsheet
1 min read
This knowledge base article explains how to enter only numeric values in a JavaScript Spreadsheet.
Check whether the entered value is a number on the cellSave event and allow the value to be updated in the cell. If the entered data is not a number, use the updateCell method to restrict the data being updated and clear the entered value.
[HTML]
<div id="spreadsheet"></div>
[TS]
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet'
let spreadsheet: Spreadsheet = new Spreadsheet({
cellSave: function (args: {value: string, requestType: string, address: string}) {
let numericValue : number = parseInt(args.value);
// Check whether the entered value contains any characters
if (Number.isNaN(numericValue)) {
// Clear the entered value.
spreadsheet.updateCell({ value: '' }, args.address);
alert('Only numbers are allowed..!');
}
}
});
spreadsheet.appendTo('#spreadsheet');
Sample Link:
https://stackblitz.com/edit/fhhh93-xebxxx?file=index.ts,index.html
Output: