Kendo UI for jQuery入門級教程:Spreadsheet - 創建基於RegExp的自定義驗證

Kendo UI for jQuery最新版工具下載

Spreadsheet的驗證類型不直接基於RegExp的規則。

要解決此問題,請使用允許您傳遞任何公式的自定義驗證類型。 當公式返回非假值時,驗證將通過。 雖然內置函數不包括 RegExp 匹配函數,但自定義函數很容易創建。

<script>
// Define a REGEXP_MATCH function that returns true if a string
// matches a given pattern (regexp).
kendo.spreadsheet.defineFunction("REGEXP_MATCH", function(str, pattern, flags){
var rx;
try {
rx = flags ? new RegExp(pattern, flags) : new RegExp(pattern);
} catch(ex) {
// could not compile regexp, return some error code
return new kendo.spreadsheet.CalcError("REGEXP");
}
return rx.test(str);
}).args([
[ "str", "string" ],
[ "pattern", "string" ],
[ "flags", [ "or", "string", "null" ] ]
]);
</script>

<div id="spreadsheet"></div>

<script>
var spreadsheet = $("#spreadsheet").kendoSpreadsheet({
columnWidth: 100
}).getKendoSpreadsheet();

var sheet = spreadsheet.activeSheet();

sheet.range("A1").value("IP Address in B1:");

// Using custom validation, you can pass any formula and the cell
// validates if the formula returns a non-false value (see the `from` field).
sheet.range("B1").validation({
comparerType: "custom",
dataType: "custom",
from: 'REGEXP_MATCH(B1, "^[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}$")'
});

// Note the difficulty of properly quoting a regexp in a string.
// An alternative would be to write the regexp in a
// variable and encode it with JSON.stringify, i.e.:
//
// var rx = "^[0-9]{1,3}\\.[0-9]{1,3}\\." etc
//
// and then pass it like this
//
// from: '=REGEXP_MATCH(B1, ' + JSON.stringify(rx) + ')'

</script>

Kendo UI for jQuery | 下載試用

Kendo UI for jQuery是完整的jQuery UI組件庫,可快速構建出色的高性能響應式Web應用程序。Kendo UI for jQuery提供在短時間內構建現在Web應用程序所需要的一切,從多個UI組件中選擇,並輕鬆地將它們組合起來,創建出酷炫響應式的應用程序,同時將開發時間加快了50%。


瞭解最新Kendo UI最新資訊,請關注Telerik中文網!

慧都高端UI界面開發
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章