Associated personal restriction symbols
I was looking for an answer to a very simple question, How do I stop the user from entering 250 characters into the Handsontable cell? I found I could recreate the validation, but that would not stop the user from entering more than 250 characters. I'm looking for something like maxlength:
<input id="notes" maxlength="250" />
var date_validator_regexp = /(^$|^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/][0-9]{4}$)/;
var limit_validator_regexp = /(^[\s\S]{0,250}$)/;
$("#gridUpdateNotes").handsontable({
startRows: 1,
startCols: 2,
colHeaders: ["Date", "Notes"],
columnSorting: false,
enterBeginsEditing: false,
autoWrapRow: true,
autoWrapCol: true,
minSpareRows: 1,
colWidths: [140, 450],
removeRowPlugin: true,
copyPaste: true,
afterValidate: function (isValid, value, row, prop, source) {
if (isValid == false && prop === "Notes") {
alert("The Notes cannot have more than 250 characters.");
}
},
columns: [
{
data: "NoteDate",
type: "date",
dateFormat: "mm/dd/yy",
allowInvalid: false,
validator: date_validator_regexp
},
{
data: "Notes",
allowInvalid: false,
validator: limit_validator_regexp
}
]
});
+3
source to share
2 answers
This question has been going on for several months, so Filjan may no longer need an answer. But hopefully this helps someone.
Instead of using a regular expression as a validator, you can use a function.
Defining a column like this worked for me ...
cmlCols = [
{
data: "Notes",
validator: function (value, callback) {
if (value.length > 255) {
alert('Must be 255 character or less. Extra characters will be removed');
this.instance.setDataAtCell(this.row, this.col, value.substring(0, 255), null);
}
callback(true);
}];
+4
source to share
I think creating a new cell editor seems like the best way to approach this:
(function (Handsontable) {
'use strict';
var MaxLengthEditor = Handsontable.editors.TextEditor.prototype.extend();
MaxLengthEditor.prototype.prepare = function () {
Handsontable.editors.TextEditor.prototype.prepare.apply(this, arguments);
this.TEXTAREA.maxLength = this.cellProperties.maxLength;
};
Handsontable.editors.MaxLengthEditor = MaxLengthEditor;
Handsontable.editors.registerEditor('maxlength', MaxLengthEditor);
})(Handsontable);
+1
source to share