@formseal/embed 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ // fse.validate.js
2
+ // Validates a data object against FSE.fields validation rules.
3
+ // No DOM access. No crypto. Pure validation logic.
4
+ // Depends on: FSE (global).
5
+
6
+ var FSEValidate = (function () {
7
+
8
+ function validate(data) {
9
+ if (typeof FSE === "undefined") {
10
+ throw new Error("[fse/validate] FSE is not defined.");
11
+ }
12
+
13
+ var fields = FSE.fields || {};
14
+ var errors = [];
15
+
16
+ Object.keys(fields).forEach(function (name) {
17
+ var field = fields[name];
18
+ var value = (data[name] || "").toString().trim();
19
+
20
+ if (field.required && value.length === 0) {
21
+ errors.push({
22
+ name: name,
23
+ message: name + " is required.",
24
+ });
25
+ return;
26
+ }
27
+
28
+ if (field.maxLength && value.length > field.maxLength) {
29
+ errors.push({
30
+ name: name,
31
+ message: name + " must be " + field.maxLength + " characters or fewer.",
32
+ });
33
+ }
34
+
35
+ if (field.type === "email" && value.length > 0) {
36
+ var emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
37
+ if (!emailRe.test(value)) {
38
+ errors.push({
39
+ name: name,
40
+ message: name + " must be a valid email address.",
41
+ });
42
+ }
43
+ }
44
+
45
+ if (field.type === "tel" && value.length > 0) {
46
+ var telRe = /^\+?[\d\s\-().]{6,20}$/;
47
+ if (!telRe.test(value)) {
48
+ errors.push({
49
+ name: name,
50
+ message: name + " must be a valid phone number.",
51
+ });
52
+ }
53
+ }
54
+ });
55
+
56
+ return {
57
+ valid: errors.length === 0,
58
+ errors: errors,
59
+ };
60
+ }
61
+
62
+ return {
63
+ validate,
64
+ };
65
+
66
+ })();