Hi,
I added possibility for a new verification of required fields in generated Javascript "function chkform() ". To do this I added new logbook option "ValidPattern", in which you can set regular expression for an attribute, for example
ValidPattern HexDigits=[0-9a-fA-F]+
If there is a pattern set for required field, then in chkform() function is generated additional verification, if value of the field matches validation regexp.
Changes I made should not decrease performance of elogd. Let me know please if you can add it to regular version.
Jacek |
Index: elogd.c
===================================================================
--- elogd.c (revision 2414)
+++ elogd.c (working copy)
@@ -74,6 +74,8 @@
char attr_list[MAX_N_ATTR][NAME_LENGTH];
char attr_options[MAX_N_ATTR][MAX_N_LIST][NAME_LENGTH];
int attr_flags[MAX_N_ATTR];
+/** Validation pattern for attribute, to test if it contains expected value */
+char attr_valid_pattern[MAX_N_ATTR][NAME_LENGTH];
char attr_list_default[][NAME_LENGTH] = { "Author", "Type", "Category", "Subject", "" };
@@ -7033,7 +7035,7 @@
int scan_attributes(char *logbook)
/* scan configuration file for attributes and fill attr_list, attr_options
- and attr_flags arrays */
+ and attr_flags and attr_valid_pattern arrays */
{
char list[10000], str[NAME_LENGTH], str2[NAME_LENGTH], type[NAME_LENGTH],
tmp_list[MAX_N_ATTR][NAME_LENGTH];
@@ -7062,11 +7064,20 @@
}
}
- /* get options lists for attributes */
+ /* get options lists and validation patterns for attributes */
memset(attr_options, 0, sizeof(attr_options));
+ memset(attr_valid_pattern, 0, sizeof(attr_valid_pattern));
for (i = 0; i < n; i++) {
n_options = 0;
+ sprintf(str, "ValidPattern %s", attr_list[i]);
+ if (getcfg(logbook, str, list, sizeof(list)))
+ {
+ strncpy(attr_valid_pattern[i], list, sizeof(attr_valid_pattern[i])-1);
+ attr_valid_pattern[i][sizeof(attr_valid_pattern[i])-1] = 0;
+ attr_flags[i] |= AF_HAS_VALID_PATT;
+ }
+
sprintf(str, "Options %s", attr_list[i]);
if (getcfg(logbook, str, list, sizeof(list)))
n_options = strbreak(list, attr_options[i], MAX_N_LIST, ",", FALSE);
@@ -9650,6 +9661,17 @@
rsprintf(" document.form1.%s.focus();\n", ua);
rsprintf(" return false;\n");
rsprintf(" }\n");
+ if (attr_flags[i] & AF_HAS_VALID_PATT)
+ {
+ sprintf(str, loc("var validPatt=new RegExp(\"%s\");"), attr_valid_pattern[i]);
+ rsprintf(" %s\n", str);
+ rsprintf(" if (!validPatt.test(document.form1.%s.value)) {\n", ua);
+ sprintf(str, loc("Invalid value for attribute '%s'"), attr_list[i]);
+ rsprintf(" alert(\"%s\");\n", str);
+ rsprintf(" document.form1.%s.focus();\n", ua);
+ rsprintf(" return false;\n");
+ rsprintf(" }\n");
+ }
}
}
Index: elogd.h
===================================================================
--- elogd.h (revision 2414)
+++ elogd.h (working copy)
@@ -192,6 +192,7 @@
#define AF_MUSERLIST (1<<13)
#define AF_USEREMAIL (1<<14)
#define AF_MUSEREMAIL (1<<15)
+#define AF_HAS_VALID_PATT (1<<16)
/* attribute format flags */
#define AFF_SAME_LINE 1
|