MVC DataAnnotations URL 验证

发布于 2024-11-30 07:13:47 字数 547 浏览 2 评论 0 原文

我正在使用 ASP.NET MVC3 并尝试使用 DataAnnotationsExtensions 验证 URL 字段

这几乎就是我所需要的。但是,它强制用户在 URL 字符串的开头添加“http://”,如果没有,它将显示以下验证消息:

The URL field is not a valid fully-qualified http, https, or ftp URL.

在数据注释扩展 URL 演示页面 它显示了一个额外的验证器UrlWithoutProtocolRequired,但我在任何地方都找不到它。

如何使用此验证器,或者如何轻松验证没有“http://”部分的 URL?

I'm using ASP.NET MVC3 and trying to validate an URL field using DataAnnotationsExtensions.

It's almost what I need. However, it forces the user to add "http://" at the beggining of the URL string, if not, it will show the following validation message:

The URL field is not a valid fully-qualified http, https, or ftp URL.

In the Data Annotations Extensions URL demo page it shows an additional validator UrlWithoutProtocolRequired, but I cannot find it anywhere.

How can I use this validator, or how can I easily validate the URL without the "http://" part?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把梦留给海 2024-12-07 07:13:47

DataAnnotationsExtensions 的无协议选项在源代码中可用,但被视为 beta 或“vNext”,并且尚未作为 NuGet 包的一部分发布。因此,如果您下载源代码并进行编译,您将看到 [Url] 属性有一个重载 [Url(requireProtocol: false)]。您可以在最新的 UrlAttribute.cs 文件 (UrlArribute.cs) 中看到这一点。另外,如果您查看 DataAnnotationsExtensions wiki,您会看到此功能计划很快发布(我我正在考虑在接下来的一两周内发布正式的下一个版本)。

The protocol-less option for DataAnnotationsExtensions is available in the source code but is considered beta or "vNext" and hasn't been released as part of the NuGet package. So if you download the source and compile you'll see the [Url] attribute has an overload [Url(requireProtocol: false)]. You can see this in the latest UrlAttribute.cs file (UrlArribute.cs). Also if you look in the DataAnnotationsExtensions wiki you'll see this feature is scheduled to be released soon (I'm thinking in the next week or two for an official next release).

梦开始←不甜 2024-12-07 07:13:47

只是为了完成这个:

从 MVC3 开始,我们现在可以使用 [URL] 验证属性。

[Required]
[Url]
public string Website { get; set; }

Just to complete this :

Since MVC3 now we can use [URL] validation attribute.

[Required]
[Url]
public string Website { get; set; }
青萝楚歌 2024-12-07 07:13:47

我找不到内置属性来匹配 URL 并接受协议作为可选。

因此,我使用了以下正则表达式验证器:

public class MediaModel
{
    public long MediaId { get; set; }
    [StringLength(60)]
    [RegularExpression(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)", ErrorMessage = "Not a valid website URL")]
    public string Website { get; set; }
    [DisplayName("YouTube Video")]
    [StringLength(200)]
    [RegularExpression(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)", ErrorMessage = "Not a valid YouTube video")]
    public string YouTubeVideo { get; set; }
}

我从 这里,这是一个很好的。

I could not find a built in attribute to match a URL and accept the protocol as optional.

So instead, I used the following RegularExpression validator:

public class MediaModel
{
    public long MediaId { get; set; }
    [StringLength(60)]
    [RegularExpression(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)", ErrorMessage = "Not a valid website URL")]
    public string Website { get; set; }
    [DisplayName("YouTube Video")]
    [StringLength(200)]
    [RegularExpression(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)", ErrorMessage = "Not a valid YouTube video")]
    public string YouTubeVideo { get; set; }
}

I copied the Regular expression from here, it is a good one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文