【问题标题】:MVC Model Validation RegExMVC 模型验证正则表达式
【发布时间】:2023-03-28 16:16:01
【问题描述】:

我需要一个正则表达式才能在没有 http:// 或 https:// 的情况下验证域名

我的意思:

有效的应该是:

domain.com
domain.fr
domain.it
domain.whateverelse
subdomain.domain.com
subdomain.doamin.fr
subdomain.domain.whateverelse

应该是无效的:

domain
http://domain.com
https://domain.com
https://domain.whateverelse
http://subdomain.domain.com
http://subdomain.domain.fr
http://subdomain.domain.whateverelse

这就是我到目前为止的结果:

(http(s)?://)?([\w-]+\.)+[\w-]+[.com]+(/[/?%&=]*)?

例如,上面的正则表达式认为 domain.whatever 无效......所以基本上只适用于 .com

【问题讨论】:

标签: regex asp.net-mvc-5 model-validation


【解决方案1】:

这个看起来不错:

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

来自:http://regexlib.com/REDetails.aspx?regexp_id=96 哪里可以找到更多;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    相关资源
    最近更新 更多