小编Zwa*_*arp的帖子

带有 Vuetify 文本字段的正则表达式 URL 规则失败

我在 Typescript 中有一个使用 Vuetify 测试 Vue 中文本字段规则的函数。

像https://www.cnn.com 、 www.test.xxx这样的东西验证失败。我似乎无法理解为什么......

Vuetify 文本字段(Vuetify v.2.2.26):

<v-text-field label="Web site" dense :rules="webSiteRules" :readonly="!editing" v-model="profile.WebSite" />
Run Code Online (Sandbox Code Playgroud)

代码(在 .vue 文件中导入 - 使用单个文件组件):

export function websiteRules(): ((v: string) => boolean | string)[] {
  const regexp = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/gim;
  return [
    (v: string): boolean | string => !!v || 'Web site required',
    (v: string): boolean | string =>
      regexp.test(v) || 'Wrong format. use e.g. http(s)://www.cnn.com'
  ];
}
Run Code Online (Sandbox Code Playgroud)

regex url typescript vuetify.js

1
推荐指数
1
解决办法
4022
查看次数

标签 统计

regex ×1

typescript ×1

url ×1

vuetify.js ×1