Jquery 可手持限制单元字符
我一直在寻找一个非常简单的问题的答案,如何阻止用户在手持设备的单元格中输入250个字符?我发现我可以重新创建验证,但这不会阻止用户输入超过250个字符。我正在寻找类似maxlength的东西:Jquery 可手持限制单元字符,jquery,handsontable,Jquery,Handsontable,我一直在寻找一个非常简单的问题的答案,如何阻止用户在手持设备的单元格中输入250个字符?我发现我可以重新创建验证,但这不会阻止用户输入超过250个字符。我正在寻找类似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_valid
<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
}
]
});
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]{0250}$)/;
$(“#GridUpdateNodes”).handsontable({
startRows:1,
startCols:2,
列标题:[“日期”,“注释”],
列排序:false,
enterBeginsEditing:false,
autoWrapRow:是的,
autoWrapCol:是的,
会议记录:1,
冷宽:[140450],
是的,
复制粘贴:对,
afterValidate:函数(isValid、值、行、属性、源){
if(isValid==false&&prop==“Notes”){
警报(“注释不能超过250个字符”);
}
},
栏目:[
{
数据:“NoteDate”,
键入:“日期”,
日期格式:“mm/dd/yy”,
allowInvalid:false,
验证程序:日期\u验证程序\u regexp
},
{
数据:“附注”,
allowInvalid:false,
验证程序:limit\u validator\u regexp
}
]
});
这个问题已经问了几个月了,因此Filjan可能不再需要答案。但希望这能帮助一些人
您可以使用函数,而不是使用正则表达式作为验证器
像这样定义专栏对我来说很有用
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);
}];
我认为创建一个新的单元格编辑器似乎是更好的方法:
(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);
与另一个答案相比,这个方法的优点在于:用户甚至不允许输入超过maxLength的字符。我不能像现在这样使用它。从未想过如何设置自定义cellProperties.maxLength,但还是设法找到了一个解决方法,将此自定义编辑器和稍微修改的自定义渲染器结合起来。