我正在使用jqGrid中的editrules属性来调用自定义函数进行验证。我使用正则表达式验证4位数年份,我相当肯定它是正确的。但是,无论在网格中编辑BNFT_YR时输入的值是什么,都会返回错误消息。这是一个异步回调问题吗?如果是这样,怎么解决?有没有更好的方法来完成我想要做的事情?谢谢你的帮助!
$(function () {
//var x = $('#' + '@(ViewBag.hohupi)').val();
var benyr,
bnftYearCheck = function (value, colname) {
var patt = /\d{4}$'/;
var intRegex = new RegExp(/\d{4}$'/);
if (colname === "BNFT_YR") {
benyr = value;
}
if (intRegex.test(benyr)) {
return [true];
}
else {
return [false, "Erorr: wrong input"];
alert('Incorrect input for BNFT_YR');
}
};
$("#grid").jqGrid({
url: "/Transactions/GetTransactions/",
datatype: 'json',
mtype: 'Get',
/* colNames: ['TransactionID', 'HOH_UPI', 'ICI', 'Run Date', 'Billed QTR', 'Benefit Year', 'Benefit Month', 'Billed Premium', 'Amount', 'Transaction Type', 'Check No.', 'Check Received Date','Entry Date', 'Name on Check', 'Indv Batch Number', 'USER_INIT','Returned Check Date', 'Late Date', 'Final Date', 'Comments'],*/
colNames: ['TRANS_ID','HOH_UPI', 'ICI', 'Run Date', 'Billed QTR', 'Benefit Year', 'Benefit Month', 'Billed Premium', 'Amount', 'Transaction Type', 'Check No.', 'Check Received Date', 'Entry Date', 'Name on Check', 'Indv Batch Number', 'USER_INIT', 'Returned Check Date', 'Late Date', 'Final Date', 'Comments'],
colModel: [
{ key: true, hidden: false, name: 'TRANS_ID', index: 'TRANS_ID', editable: true },
{ key: false, name: 'HOH_UPI', index: 'HOH_UPI', editable: true },
{ key: false, name: 'ICI', index: 'ICI', editable: true },
{ key: false, name: 'RUN_DT', index: 'RUN_DT', editable: true, formatter: 'date', formatoptions: { newformat: 'Y-m-d H:i:s' } },
{ key: false, name: 'BILL_QTR', index: 'BILL_QTR', editable: true },
{ key: false, name: 'BNFT_YR', index: 'BNFT_YR', editable: true, editrules: {custom: true, custom_func: bnftYearCheck} },
答案 0 :(得分:3)
尝试使用
var bnftYearCheck = function (value, colname) {
if (/^\d{4}$/.test(value)) {
return [true];
} else {
return [false, "Incorrect input for the '" + colname + "' column."]
}
};