PageSourceSearch

https://www.samaterials.es/js/utils.js?v=1788571303

js samaterials.es collected 2026-10-02 20:46:51 UTC 7,677 bytes, 224 lines download raw bytes

1/**
2 * 校验表单字段是否符合要求
3 * @param {Array|Object} validationform 校验规则,格式为
4 * [
5        {
6            required?:bloolean, 默认为true
7            key:string,         必填-字段名
8            value:string,       必填-字段值
9            message?:string,    错误提示 等价于 errorMessage.required
10            maxLength?:number,  最大长度
11            validate?:function, 自定义校验函数
12            regex?:string,      正则表达式
13            invalidWords?:Array 不允许出现的字符
14            errorMessage?:Object 错误提示
15        }
16        ...
17    ]
18 * @returns {Object} 校验结果对象,格式为
19    {
20        isValid: boolean,    校验是否通过
21        message: string,      错误提示
22        values: Object,      字段值
23    }
24 */
25    function validateForm(validationform) {
26        const valValid = {
27          isValid: true,
28          errorKey: null, // 👈 新增
29          values: {},
30        }
31        if(!Array.isArray(validationform)){
32            validationform = [validationform];
33        }
34        // 不允许出现的字符
35        const disallowedChars =[
36            "http",
37            "https",
38            "loan",
39            "loans",
40            "debt",
41            "debts",
42            "essay",
43            "essays",
44            "???",
45        ]
46
47        // 固定校验字段
48        const validationRules = [
49            {
50                key: "product",
51                maxLength: 200,
52                invalidWords: disallowedChars,
53                required: true,
54                errorMessage: {
55                    required: TextPrompt.productName,
56                    maxLength: TextPrompt.productLength,
57                    invalidWords: TextPrompt.productInvalidWord,
58                },
59            },
60            {
61                key: "username",
62                maxLength: 50,
63                invalidWords: disallowedChars,
64                required: true,
65                errorMessage: {
66                    required: TextPrompt.name,
67                    maxLength: TextPrompt.nameLength,
68                    invalidWords: TextPrompt.nameInvalidWord,
69                },
70            },
71            {
72                key: "phone",
73                maxLength: 50,
74                regex: /^[0-9+\-\(\))]{1,50}$/,
75                required: true,
76                errorMessage: {
77                    required: TextPrompt.phone,
78                    invalid: TextPrompt.phoneLength,
79                },
80            },
81            {
82                key: "email",
83                maxLength: 100,
84                regex: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
85                required: true,
86                errorMessage: {
87                    required: TextPrompt.yourEmail,
88                    maxLength: TextPrompt.emailLength,
89                    invalid: TextPrompt.emailInvalidWord,
90                },
91            },
92            {
93                key: "message",
94                maxLength: 500,
95                required: true,
96                errorMessage: {
97                    required: TextPrompt.comment,
98                    maxLength: TextPrompt.commentsLength,
99                },
100            },
101        ];
102
103        validationform.forEach(v=>{
104
105        })
106        for (const index in validationform) {
107            // required 默认为true
108            if (!("required" in validationform[index])) {
109                validationform[index].required = true;
110            }
111            let rule = validationRules.find(
112                (rule) => rule.key === validationform[index].key
113            );
114            if (rule) {
115                validationform[index] = { ...rule, ...validationform[index] };
116            }
117            if (validationform[index].message) {
118                if (!validationform[index].errorMessage) {
119                    validationform[index].errorMessage = {};
120                }
121                validationform[index].errorMessage.required =
122                    validationform[index].message;
123            }
124        }
125
126        for (const [index, rule] of Object.entries(validationform)) {
127            const value = (rule.value+"" || "").trim();
128            const errorMessage = rule.errorMessage;
129            if (rule.required && !value) {
130                valValid.message = errorMessage.required;
131                valValid.errorKey = rule.key
132                valValid.isValid = false;
133            } else if (!rule.required && !value) {
134                // 如果不是必填项且值为空,则跳过所有校验
135                continue;
136            } else if (rule.maxLength && value.length > rule.maxLength) {
137                valValid.message = errorMessage.maxLength;
138                valValid.errorKey = rule.key
139                valValid.isValid = false;
140            }
140 else if (rule.invalidWords && containsInvalidWords(value, rule.invalidWords)) {
141                valValid.message = errorMessage.invalidWords;
142                valValid.errorKey = rule.key
143                valValid.isValid = false;
144            } else if (rule.regex && !rule.regex.test(value)) {
145                valValid.message = errorMessage.invalid;
146                valValid.errorKey = rule.key
147                valValid.isValid = false;
148            } else if (typeof rule.validate == "function" && !rule.validate(value)) {
149                let { validate, required } = rule.errorMessage;
150                valValid.message = validate || required;
151                valValid.errorKey = rule.key
152                valValid.isValid = false;
153            }
154            valValid.values[rule.key] = value;
155            if(!valValid.isValid){
156                break;
157            }
158        }
159
160        return valValid;
161    }
162
163    // 辅助函数:检查字符串中是否包含无效单词
164    function containsInvalidWords(
165        str,
166        invalidWords
167    ) {
168        return invalidWords.some((word) => str.toLowerCase().includes(word));
169    }
170
171    // 辅助函数:校验电话号码
172    function isValidPhone(phone) {
173        const phoneRegex = /^[0-9+\-$$]{1,50}$/;
174        return phoneRegex.test(phone);
175    }
176
177    // 辅助函数:校验电子邮件地址
178    function isValidEmail(email) {
179        const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
180        return emailRegex.test(email);
181    }
182
183
184    function showToast(msg, type = "success") {
185    let toastHtml = "";
186    if (type == "success") {
187        toastHtml = `<div class="oc-toast oc-toast-success">
188            ${msg}
189        </div>`;
190    }
191    // 将 toast 添加到页面
192    $("body").append(toastHtml);
193
194    // 获取新添加的 toast 元素
195    const $toast = $(".oc-toast").last();
196
197    // 3 秒后删除 toast
198    setTimeout(function () {
199        $toast.fadeOut(300, function () {
200            $(this).remove();
201        });
202    }, 3000);
203}
204
205/**
206 * 获取Google Recaptcha 验证码
207 * @returns Google Recaptcha
208 */
209function getGoogleSafeResponse() {
210    let response = "";
211    try {
212        // 尝试获取第一个,如果没有值,尝试获取第二个
213        // 注意:只有当 0 存在且有值时,才会返回;否则尝试 1
214        response = grecaptcha.getResponse(0) || grecaptcha.getResponse(1);
215    } catch (e) {
216        // 如果索引 1 不存在,捕获错误防止程序崩溃
217        try {
218            response = grecaptcha.getResponse(0);
219        } catch (e2) {
220            response = "";
221        }
222    }
223    return response;
224}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.