PageSourceSearch

https://www.samaterials.com/js/utils.js?v=1788571253

js samaterials.com collected 2026-09-24 17:59:11 UTC 6,259 bytes, 215 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 */
25function 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  for (const index in validationform) {
105    // required 默认为true
106    if (!('required' in validationform[index])) {
107      validationform[index].required = true
108    }
109    let rule = validationRules.find((rule) => rule.key === validationform[index].key)
110    if (rule) {
111      validationform[index] = { ...rule, ...validationform[index] }
112    }
113    if (validationform[index].message) {
114      if (!validationform[index].errorMessage) {
115        validationform[index].errorMessage = {}
116      }
117      validationform[index].errorMessage.required = validationform[index].message
118    }
119  }
120
121  for (const [index, rule] of Object.entries(validationform)) {
122    const value = (rule.value + '' || '').trim()
123    const errorMessage = rule.errorMessage
124    if (rule.required && !value) {
125      valValid.message = errorMessage.required
126      valValid.errorKey = rule.key
127      valValid.isValid = false
128    } else if (!rule.required && !value) {
129      // 如果不是必填项且值为空,则跳过所有校验
130      continue
131    } else if (rule.maxLength && value.length > rule.maxLength) {
132      valValid.message = errorMessage.maxLength
133      valValid.errorKey = rule.key
134      valValid.isValid = false
135    } else if (rule.invalidWords && containsInvalidWords(value, rule.invalidWords)) {
136      valValid.message = errorMessage.invalidWords
137      valValid.errorKey = rule.key
138      valValid.isValid = false
139    } else if (rule.regex && !rule.regex.test(value)) {
140      valValid.message = errorMessage.invalid
141      valValid.errorKey = rule.key
142      valValid.isValid = false
143    } else if (typeof rule.validate == 'function' && !rule.validate(value)) {
144      let { validate, required } = rule.errorMessage
145      valValid.message = validate || required
146      valValid.errorKey = rule.key
147      valValid.isValid = false
148    }
149    valValid.values[rule.key] = value
150    if (!valValid.isValid) {
151      break
152    }
153  }
154
155  return valValid
156}
157
158// 辅助函数:检查字符串中是否包含无效单词
159function containsInvalidWords(str, invalidWords) {
160  return invalidWords.some((word) => str.toLowerCase().includes(word))
161}
162
163// 辅助函数:校验电话号码
164function isValidPhone(phone) {
165  const phoneRegex = /^[0-9+\-$$]{1,50}$/
166  return phoneRegex.test(phone)
167}
168
169// 辅助函数:校验电子邮件地址
170function isValidEmail(email) {
171  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
172  return emailRegex.test(email)
173}
174
175function showToast(msg, type = 'success') {
176  let toastHtml = ''
177  if (type == 'success') {
178    toastHtml = `<div class="oc-toast oc-toast-success">
179            ${msg}
180        </div>`
181  }
182  // 将 toast 添加到页面
183  $('body').append(toastHtml)
184
185  // 获取新添加的 toast 元素
186  const $toast = $('.oc-toast').last()
187
188  // 3 秒后删除 toast
189  setTimeout(function () {
190    $toast.fadeOut(300, function () {
191      $(this).remove()
192    })
193  }, 3000)
194}
195
196/**
197 * 获取Google Recaptcha 验证码
198 * @returns Google Recaptcha
199 */
200function getGoogleSafeResponse() {
201  let response = ''
202  try {
203    // 尝试获取第一个,如果没有值,尝试获取第二个
204    // 注意:只有当 0 存在且有值时,才会返回;否则尝试 1
205    response = grecaptcha.getResponse(0) || grecaptcha.getResponse(1)
206  } catch (e) {
207    // 如果索引 1 不存在,捕获错误防止程序崩溃
208    try {
209      response = grecaptcha.getResponse(0)
210    } catch (e2) {
211      response = ''
212    }
213  }
214  return response
215}

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.