Skip to content

Commit 41fcc32

Browse files
committed
fix: 修复网站匹配错误的 bug
1 parent 886ea01 commit 41fcc32

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/client/view/submit.svelte

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828
const mailStr = 'mail'
2929
const siteStr = 'site'
3030
const contentStr = 'content'
31-
// source: /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]{1,30}\.)+[A-Za-z\d]{2,5}$/
32-
const redo = '[A-Za-z\\d]'
33-
const domain = `(${redo}{1,30}\\.)+${redo}{2,5}$`
3431
const mailReg = /^\w+([-.]\w+)*@\w+([-.]\w+)*(\.[a-z]{2,5})+$/
35-
const siteReg = new RegExp('^https?://' + domain)
32+
33+
function isUrl(str) {
34+
try {
35+
if (/^https?:\/\//.test(str) && /([A-Za-z\d]{1,30}\.)+[A-Za-z\d]{2,5}$/.test(new URL(str).hostname)) {
36+
return true
37+
}
38+
// eslint-disable-next-line no-empty
39+
} catch (error) {}
40+
return false
41+
}
3642
3743
// svelte变量
3844
let storage = localStorage.discuss
@@ -192,7 +198,7 @@
192198
let condition = true
193199
if (k === nickStr) condition = len > 1
194200
if (k === mailStr) condition = mailReg.test(v.value)
195-
if (k === siteStr) condition = len === 0 ? true : siteReg.test(v.value)
201+
if (k === siteStr) condition = len === 0 ? true : isUrl(v.value)
196202
if (k === contentStr) condition = len > 1
197203
198204
// 如果word的参数不为0,则判断输入框内容长度是否符合规定

src/server/utils/commentUtils.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,23 @@ async function CommitCommentHandler(params) {
257257
* @param {*} site 网站
258258
*/
259259
function VerufyMailANDSite(mail, site) {
260-
/*
261-
source:
262-
/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]{1,30}\.)+[A-Za-z\d]{2,5}$/
263-
*/
264-
const redo = '[A-Za-z\\d]'
265-
const domain = `(${redo}{1,30}\\.)+${redo}{2,5}$`
260+
function isUrl(str) {
261+
try {
262+
if (/^https?:\/\//.test(str) && /([A-Za-z\d]{1,30}\.)+[A-Za-z\d]{2,5}$/.test(new URL(str).hostname)) {
263+
return true
264+
}
265+
// eslint-disable-next-line no-empty
266+
} catch (error) {}
267+
return false
268+
}
266269
const mailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*(\.[a-z]{2,5})+$/
267-
const siteReg = new RegExp('^https?://' + domain)
268270

269271
const errorMail = 'Mail format does not meet the requirements!'
270272
const errorSite = 'Site address format does not meet the requirements!'
271273
if (!mailReg.test(mail)) throw new Error(errorMail)
272274

273-
const condition = site.length !== 0 && !siteReg.test(site)
274-
if (condition) throw new Error(errorSite)
275+
if (site.length === 0) return
276+
if (!isUrl(site)) throw new Error(errorSite)
275277
}
276278

277279
// 限流

0 commit comments

Comments
 (0)