在我的AEM touch ui对话框中,我有一个文本字段。在该文本字段中,作者可以输入这样的数据。 " [$]"或" {$}"或"($)"。现在我的要求是验证开始和结束括号是否相同。我的意思是如果作者已经[$],那么我想验证[(开括号)和](右括号)是否相同。任何指针都将受到高度赞赏。
答案 0 :(得分:2)
您可以为此使用基础验证库。这样,您可以在项目中的所有组件之间重用验证。就像添加新属性一样简单。
这已在AEM 6.3 / 6.4上进行了测试,但也应在6.2上使用。
这也适用于使用花岗岩组件和珊瑚库创建的字段。根据需求的复杂性,花岗岩和珊瑚组件需要进行细微的更改。
步骤:
如果需要验证多字段(例如文本字段)中的字段,则可以编写文本字段,并且只需在字段节点上使用相应的选择器即可实现验证,如下所示。
下面的代码验证文本字段以检查某些预设模式。预设模式可以在/libs/granite/ui/components/foundation/form/formbuilder/clientlibs/js/form-validation.js
中找到 (function($, window, document) {
/* Adapting window object to foundation-registry */
var registry = $(window).adaptTo("foundation-registry");
/*Validator for TextField - Any Custom logic can go inside validate function - starts */
registry.register("foundation.validation.validator", {
selector: "[data-validation=txt-validate]",
validate: function(el) {
var element = $(el);
var pattern = element.data('pattern');
var value = element.val();
if (value.length == 0) {
return "Please enter text";
} else {
patterns = {
phone: /([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/,
email: /((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-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A-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?/,
// number: /-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?/,
number: /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/,
dateISO: /\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,
alpha: /[a-zA-Z]+/,
alphaNumeric: /\w+/,
integer: /-?\d+/,
systemKey: /^[-_a-z0-9]+$/i
}
/*
* Test pattern if set. Pattern can be a preset regex pattern name or
* a regular expression such as "^\\d+$".
*/
if (pattern) {
if (patterns[pattern]) {
error = !patterns[pattern].test(value);
} else {
error = !(new RegExp(pattern)).test(value);
}
if (error) {
return "The field must match the pattern: " + pattern;
}
}
}
}
});
})
($, window, document);
示例1: 检查数据是否为空并且包含URL。
输出:
示例2: 检查数据是否为空,是否包含珊瑚多字段内文本字段的字母。 多字段资源类型:granite / ui / components / coral / foundation / form / multifield
为什么我会推荐这个: