jqGrid 编辑规则 属性 自定义函数正则表达式验证

jqGrid Edit Rules Property Custom Function Regex Validation

我在 jqGrid 中使用编辑规则 属性 调用自定义函数进行验证。我正在使用正则表达式来验证 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} },

尝试使用

var bnftYearCheck = function (value, colname) {
        if (/^\d{4}$/.test(value)) {
            return [true];
        } else {
            return [false, "Incorrect input for the '" + colname + "' column."]
        }
    };